# How can I send localstorage data from JavaScript ajax call and print return data from views in Django template

**URL:** <https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727>\
**Category:** Using Django\
**Created:** [January 21, 2022, 9:05pm UTC](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727 "2022-01-21T21:05:13Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 23, 2022, 5:21pm UTC](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727/21 "2022-01-23T17:21:07Z")

</div>

Have you done this?

> [@KenWhitesell](#):
>
> Next steps I would take would be to verify that `compare_ids` is a list of the right datatypes matching your id field of `Products` and that your `products` queryset is returning the expected results.

You can also use the Django shell to test whether the `compare_ids` you’re receiving generates the proper results with your query.

---

<div class="post-metadata">

**Author:** ![marly](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@marly](https://forum.djangoproject.com/u/marly)\
**Post date:** [January 23, 2022, 5:42pm UTC](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727/22 "2022-01-23T17:42:40Z")

</div>

Yes its printing \<QuerySet [\<Products: Test product\>, \<Products: STEIF WELDING HELMET With CONNECTOR\>]\> when I print Products context.

But its not visible on template even I am doing on correct way. When I check on network inside console on browser data is there but its not visible on site. And header inside Networks shows method as GET

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 23, 2022, 5:51pm UTC](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727/23 "2022-01-23T17:51:29Z")

</div>

If, in your view, printing `context['products']` is showing you the correct data, then the view is working as expected and the problem is in the template.

---

<div class="post-metadata">

**Author:** ![marly](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@marly](https://forum.djangoproject.com/u/marly)\
**Post date:** [January 23, 2022, 5:54pm UTC](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727/24 "2022-01-23T17:54:54Z")

</div>

In template when /compare page loads, it sends POST data from ajax and I am trying to publish data on template obtained from views. But I think the data obtained from views is not printed, its just printing passed from **else** condition where there is not product/context passed. How can I pass product/context obtained from **if** condition to else condition?

Or is there any better way I can resolve this?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 23, 2022, 5:56pm UTC](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727/25 "2022-01-23T17:56:37Z")

</div>

I don’t understand what you’re trying to say here.

When you wrote:

> [@marly](#):
>
> Yes its printing \<QuerySet [\<Products: Test product\>, \<Products: STEIF WELDING HELMET With CONNECTOR\>]\> when I print Products context.

How were you determining this? What code did you add to your view, and where?

---

<div class="post-metadata">

**Author:** ![marly](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@marly](https://forum.djangoproject.com/u/marly)\
**Post date:** [January 23, 2022, 6:01pm UTC](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727/26 "2022-01-23T18:01:50Z")

</div>

```auto
def compare(request):
    is_ajax = request.headers.get('X-Requested-With') == 'XMLHttpRequest'
    if is_ajax and request.method == "POST":             
        data = json.loads(request.body)     
        compare_ids = data['itemIds']      
        products = Products.objects.filter(id__in=compare_ids)   
        print(products)   
        context={'products':products}
        return render(request,'./ecommerce/compare.html', context)
    else:
     return render(request,'./ecommerce/compare.html')

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 23, 2022, 6:19pm UTC](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727/27 "2022-01-23T18:19:28Z")

</div>

If that `print` function is being called, then you’re definitely in the `if` condition side and _not_ the `else` condition, and therefore the problem **is** in the template.

---

<div class="post-metadata">

**Author:** ![marly](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@marly](https://forum.djangoproject.com/u/marly)\
**Post date:** [January 23, 2022, 6:29pm UTC](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727/28 "2022-01-23T18:29:52Z")

</div>

and I am just trying to print

```auto
                    {{product.name}}
                    {%endfor%}

```

but its not showing anything. When I check inside network, it shows data:  
as below image

 ![network](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/6/62f9c6b14c17430a51bf91614527096a522bfd49.jpeg)[quote=“marly, post:28, topic:11727, full:true”]  
and I am just trying to print

```auto
{% for product in products%}
                    {{product.name}}
                    {%endfor%}

```

but its not showing anything. When I check inside network, it shows data:  
as below image

 ![network](https://us1.discourse-cdn.com/flex026/uploads/djangoproject/original/2X/6/62f9c6b14c17430a51bf91614527096a522bfd49.jpeg)  
[/quote]

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 23, 2022, 6:32pm UTC](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727/29 "2022-01-23T18:32:29Z")

</div>

We would need to see the complete template, and any base templates that it extends.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 23, 2022, 6:40pm UTC](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727/31 "2022-01-23T18:40:52Z")

</div>

How about your `Products` model. What does that look like? (I thought I had seen it in an earlier comment but I can’t seem to find it now. My apologies if I’m just overlooking it.)

Also, I am aware of some tags being “whitespace” sensitive to some degree. It might be worth cleaning up the whitespace in your tags. (e.g. `{% block content %}` instead of `{% block content%}` No idea on whether this this a contributing factor or not, but it’s an easy thing to try.)

---

<div class="post-metadata">

**Author:** ![marly](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@marly](https://forum.djangoproject.com/u/marly)\
**Post date:** [January 23, 2022, 6:48pm UTC](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727/32 "2022-01-23T18:48:26Z")

</div>

```auto
class Products(models.Model):
    name = models.CharField(max_length=250,null=True, blank=True,)
    slug = models.SlugField(max_length=200, unique=True,null=True)
    short_description = HTMLField(help_text = "Apply bullet list to short description")
    description = HTMLField(help_text = "Apply paragraph and bullet list to description for better visibility")
    category = models.ForeignKey(Categories, related_name="products",on_delete=models.SET_NULL,null=True,blank=True,)
    brand = models.ForeignKey(Brands,on_delete=models.CASCADE, default=None, null=True, blank=True,)
    warranty_support = HTMLField()
    product_type = models.CharField(choices=PRODUCT_TYPE, default='simple', max_length=50)
    is_published =models.BooleanField(default=False)
    timestamp = models.DateTimeField(auto_now_add=True, auto_now=False)
    updated = models.DateTimeField(auto_now_add=False, auto_now=True)

```

I can see product being called inside console, Networks as I have shared screenshot previously but not printed on template I wonder with this odd problem

---

<div class="post-metadata">

**Author:** ![marly](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@marly](https://forum.djangoproject.com/u/marly)\
**Post date:** [January 29, 2022, 10:15am UTC](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727/33 "2022-01-29T10:15:59Z")

</div>

Problem not resolved yet

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 29, 2022, 11:19am UTC](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727/34 "2022-01-29T11:19:17Z")

</div>

Looking over this entire thread, why are you returning html to an Ajax call looking for a json response?

---

<div class="post-metadata">

**Author:** ![marly](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@marly](https://forum.djangoproject.com/u/marly)\
**Post date:** [January 29, 2022, 1:33pm UTC](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727/35 "2022-01-29T13:33:39Z")

</div>

Let me explain what exactly I want to achieve with this. I have or will have some data stored on localstorage. Now I do not want those data to be stored on database but I want to show more details about those data/id on another link. So that, when that dedicated link is loaded, ajax call will happen with document.ready() , I will get id of the item, I will create filter from the obtained id from ajax. now I want to show the detail of those id obtained from ajax to that dedicated link.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 29, 2022, 1:45pm UTC](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727/36 "2022-01-29T13:45:38Z")

</div>

> [@marly](#):
>
> I have or will have some data stored on localstorage.

What precisely do you mean by “localstorage”? Are you referring to data stored in the browser?

> [@marly](#):
>
> now I want to show the detail of those id obtained from ajax to that dedicated link.

That’s fine. What do you want the view to return? HTML or JSON?

---

<div class="post-metadata">

**Author:** ![marly](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@marly](https://forum.djangoproject.com/u/marly)\
**Post date:** [January 29, 2022, 1:49pm UTC](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727/37 "2022-01-29T13:49:13Z")

</div>

Yes, loclastorage means data stored in browser. I want to print data in above template which I have shared earlier. I am trying with HTML but its not happening. I will appreciate your solution guide/recommendation.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 29, 2022, 1:49pm UTC](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727/38 "2022-01-29T13:49:53Z")

</div>

Your ajax call is set up expecting a JSON response.

---

<div class="post-metadata">

**Author:** ![marly](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@marly](https://forum.djangoproject.com/u/marly)\
**Post date:** [January 29, 2022, 1:53pm UTC](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727/39 "2022-01-29T13:53:31Z")

</div>

Should I do anything to expect HTML response? Else, how can I pass JSON via views and print back to template?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [January 29, 2022, 4:59pm UTC](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727/40 "2022-01-29T16:59:17Z")

</div>

It’s your decision with how you want to handle this. You need to make the choice of either having the view render the data and then injecting the html response into the DOM, or getting the response back as JSON, and using that data to populate the DOM.

Django works equally well either way.

Regarding:

> [@marly](#):
>
> Should I do anything to expect HTML response?

and assuming you’re using JQuery, see the docs at [jQuery.ajax() | jQuery API Documentation](https://api.jquery.com/jquery.ajax/)

---

<div class="post-metadata">

**Author:** ![marly](https://avatars.discourse-cdn.com/v4/letter/m/90db22/32.png) [@marly](https://forum.djangoproject.com/u/marly)\
**Post date:** [January 29, 2022, 5:13pm UTC](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727/41 "2022-01-29T17:13:55Z")

</div>

You might have seen on above code, I am trying to inject with DOM. Can you help me why I am failing?

[Previous page](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727.md?page=1)

[Next page](https://forum.djangoproject.com/t/how-can-i-send-localstorage-data-from-javascript-ajax-call-and-print-return-data-from-views-in-django-template/11727.md?page=3)
