Currently I have a view which I render to a template and return it two query lists. My view is as shown below
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 < 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))
In my template I iterate through the two lists and give a background color to the list objects which are new as
<ul id="notification_list">
<li id="first"></li>
{% for notice in New_Notice %}
<li class="new"> <a href="{{notice.n_sender.profile.get_absolute_url}}">{{ notice.n_sender.profile.url_name}} </a> {{notice.message}} your <a href="{{notice.object_url}}"> {{notice.object_type}}</a></li>
{% endfor %}
{% for notice in Old_Notices %}
<li> <a href="{{notice.n_sender.profile.get_absolute_url}}">{{ notice.n_sender.profile.url_name}} </a> {{notice.message}} your <a href="{{notice.object_url}}"> {{notice.object_type}}<a/></li>
{% endfor %}
</ul>
Now I want to do the same thing via an AJAX 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 object_lists. I know how to serialize an object list to JSON
data = serializers.serialize('json', notifications.objects.all())
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?
Please help