目前我有一個視圖,我呈現給模板並返回兩個查詢列表。我的觀點是如下圖所示如何在django的AJAX請求中返回多個JSON對象
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))
在我的模板我這兩個表進行迭代,並給出一個背景色爲這是新的列表對象作爲
<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>
現在我想要做同樣的事情通過調用AJAX
並將這些對象顯示在下拉列表中而不是新頁面上,以便用戶可以在不瀏覽的情況下在其中查看通知。我無法理解我可以如何發送兩個JSON編碼爲object_lists
。我知道如何序列化一個對象列表,JSON
data = serializers.serialize('json', notifications.objects.all())
但我可以派兩名object_lists這樣?另外我不知道如何在html中顯示這個JSON編碼的對象列表。我可以像訪問模板中的對象一樣訪問它嗎?
請幫
你還可以告訴我如何在我的AJAX成功調用中訪問這些對象,並將它們添加到正確的html標記 – Sachin 2012-01-16 20:17:52
另一件事是'n_sender'我可能不需要整個對象,但我需要發件人的get_absolute_url和更多字段。我知道我需要添加到我發送的字典中,但是如何將這些值附加到我獲得的'query_set'。你能告訴我如何構建一個字典,該字典具有query_set的名稱 - 值對和來自相同字典中相應的'n_sender'對象的必需名稱值對 – Sachin 2012-01-16 20:29:38