Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

234
Views
How to Parse LocalStorage JSON object in Django View from Ajax?

I am trying to submit localstorage data via a POST request using the below jquery ajax method. How should I write my view so I can Parse my JSON object and get a hold of "product_id" to execute the below command in my Django view. Please see a copy of my view below.

Trying since one week, but I failed to fix the issue

Is there any better way of achieving this ?

My Ajax:

   $(document).ready(function() {
        var compare = localStorage.getItem("comparisionItems");
        var compareObj = JSON.parse(compare);
        
        var data_url = window.location.href;
        console.log(compare)
        console.log(compareObj)
        
       
          
          $.ajax({
            url: data_url,
            type: "POST",
            data: {'compare_id': compareObj },
            headers: { "X-CSRFToken": $.cookie("csrftoken") },
            success: function (result) {
                console.log("Success")
                
            },
        
            
        });
    });

and My Views:

def compare(request):
is_ajax = request.headers.get('X-Requested-With') == 'XMLHttpRequest'
if is_ajax and request.method == "POST":
    compare_id= request.POST.getlist('compare_id[itemIds]')
   
    product = get_object_or_404(Products, id=compare_id)
    context={ 'product':product}
   
   
    return render (request, './compare.html', context)

Actually my localStorage is on following format:

("comparisionItems"({ images: products, itemIds: itemIds }));

Can you please help me how can I pass itemIds to views and return item from views for the itemsIds?

Console log for console.log(compareObj) https://imgur.com/MxdZrgy

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

since .is_ajax() is deprecated you cant use that, but you can check if the request is an XMLHttpRequest like below.

from django.shortcuts import get_object_or_404

def compare(request):
    is_ajax = request.headers.get('X-Requested-With') == 'XMLHttpRequest'
    if is_ajax and request.method == "POST":
        compare_id = request.POST.get('compare_id')
        product = get_object_or_404(Products, product_id=id)
        context={ 'product':product,}
        return render (request, './ecommerce/compare.html', context)

note; the get_object_or_404 is just a shortcut for:

try:
    product = Products.objects.get(product_id=id)
except:
    raise Http404
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!