Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to return multiple JSON objects in an AJAX request in django
    primarykey
    data
    text
    <p>Currently I have a view which I render to a template and return it two query lists. My view is as shown below</p> <pre><code>def view_notifications(request,user_id): context_instance=RequestContext(request) user = User.objects.get(pk=user_id) user.profile.notifications = 0 user.profile.save() notices = list(notifications.objects.filter(n_reciever=user.id, is_read=0).order_by('-time')) number = notifications.objects.filter(n_reciever=user.id, is_read=0).order_by('-time').count() if number &lt; 5: old_notices = list(notifications.objects.filter(n_reciever=user.id, is_read=1).order_by('-time')[:5]) else: old_notices = False notifications.objects.all().update(is_read = 1) return render_to_response('profiles/notifications.html', {'New_Notice': notices, 'Old_Notices':old_notices, 'number': number,},context_instance=RequestContext(request)) </code></pre> <p>In my template I iterate through the two lists and give a background color to the list objects which are new as</p> <pre><code>&lt;ul id="notification_list"&gt; &lt;li id="first"&gt;&lt;/li&gt; {% for notice in New_Notice %} &lt;li class="new"&gt; &lt;a href="{{notice.n_sender.profile.get_absolute_url}}"&gt;{{ notice.n_sender.profile.url_name}} &lt;/a&gt; {{notice.message}} your &lt;a href="{{notice.object_url}}"&gt; {{notice.object_type}}&lt;/a&gt;&lt;/li&gt; {% endfor %} {% for notice in Old_Notices %} &lt;li&gt; &lt;a href="{{notice.n_sender.profile.get_absolute_url}}"&gt;{{ notice.n_sender.profile.url_name}} &lt;/a&gt; {{notice.message}} your &lt;a href="{{notice.object_url}}"&gt; {{notice.object_type}}&lt;a/&gt;&lt;/li&gt; {% endfor %} &lt;/ul&gt; </code></pre> <p>Now I want to do the same thing via an <code>AJAX</code> call and display these objects in a drop down list instead of a new page, so that the user can view the notifications there itself without navigating away. I cannot understand how I can send two JSON encoded <code>object_lists</code>. I know how to serialize an object list to JSON</p> <pre><code>data = serializers.serialize('json', notifications.objects.all()) </code></pre> <p>But can I send two object_lists this way? Also I do not know how to display this JSON encoded object list in html. Can I access it the same way I access the objects in the template?</p> <p>Please help</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload