2012-01-16 98 views
2

目前我有一個視圖,我呈現給模板並返回兩個查詢列表。我的觀點是如下圖所示如何在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編碼的對象列表。我可以像訪問模板中的對象一樣訪問它嗎?

請幫

回答

2

我將通過通知啓動一個空列表,循環和字典追加到包含列表所有必需的屬性。例如,您new_notices列表可能是這樣的:

[{'absolute_url': 'blah', 'url_name': 'blah', 'message': 'blah', 'type': 'blah'}, 
{'absolute_url': 'foo', 'url_name': 'foo', 'message': 'foo', 'type': 'foo'}] 

一旦你創建了每組通知(新老)的列表,你可以向他們發送:

from django.utils import simplejson 
from django.http import HttpResponse 
... 
json = simplejson.dumps({'old': old_notices, 'new': new_notices}) 
return HttpResponse(json, mimetype='text/json') 
+0

你還可以告訴我如何在我的AJAX成功調用中訪問這些對象,並將它們添加到正確的html標記 – Sachin 2012-01-16 20:17:52

+0

另一件事是'n_sender'我可能不需要整個對象,但我需要發件人的get_absolute_url和更多字段。我知道我需要添加到我發送的字典中,但是如何將這些值附加到我獲得的'query_set'。你能告訴我如何構建一個字典,該字典具有query_set的名稱 - 值對和來自相同字典中相應的'n_sender'對象的必需名稱值對 – Sachin 2012-01-16 20:29:38

3

要發送一個列表,其中包括兩個內容,一是爲每個列表中。您可以先通過序列化到Python來完成此操作,然後將整批轉儲到JSON。

data1 = serializers.serialize('python', notifications.objects.all()) 
data2 = serializers.serialize('python', foobar.objects.all()) 

data = simplejson.dumps([data1, data2]) 

(或者你可以使用一個單詞表,如果查找的關鍵是在你的JavaScript更容易。)

+0

哇不過如此我要發佈的內容,雖然你*絕對*想使用字典而不是列表。 – 2012-01-16 17:37:55

+0

這會捕獲外鍵的方法,例如'notice.n_sender.profile.get_absolute_url'? – dgel 2012-01-16 17:49:50

+0

通過鍵查找肯定會更容易,我是Python的新手,所以請你告訴我如何創建字典,其次我需要外鍵的完整對象。在這種情況下,我需要'n_sender'的整個對象 – Sachin 2012-01-16 18:51:55