尝试将此ajax插件应用于djangohttps://github.com/yceruto/django-ajax 我已成功使用ajaxPost,但无法应用ajaxGet
在视图.py. 在python中,所有数据都打印得很好。在
@ajax
def notify(request):
notifications = Notification.objects.filter(whom=request.user.profile)
for acd in notifications:
print(acd)
print(acd.choice_afl)
print(acd.whom)
print(acd.who_did)
return {'notifications': notifications}
在html中:
^{pr2}$如果我警告noty
我得到"notifications"
,如果我警告acd.choice_afl
我得到undefined
(或者如果插入到html中就什么都没有),尽管在python中我得到了我需要的结果。怎么了?python对象不转换成JSON对象吗?在
编辑:为了安全起见,我将django对象序列化为json,如下所示。没有变化
data = serializers.serialize("json",notifications)
return {'notifications': data}
编辑2:我观察了JSON的结构
var jsonPretty = JSON.stringify(JSON.parse(data),null,2);
$('#the_very_nw').html(jsonPretty);
看起来像这样。所以我错过了“田野”,但它什么也没改变。还是什么也没印出来。如果我返回HttpResponse:
[ { "fields": { "how_much_new_notifications": 0, "number_of_new_notifications": 0, "who_did": 2, "question_answered_object": null, "whom": 1, "choice_afl": "F" }, "model": "blog.notification", "pk": 1 }, { "fields": { "how_much_new_notifications": 0, "number_of_new_notifications": 0, "who_did": 2, "question_answered_object": null, "whom": 1, "choice_afl": "F" }, "model": "blog.notification", "pk": 2 }, { "fields": { "how_much_new_notifications": 0, "number_of_new_notifications": 0, "who_did": 2, "question_answered_object": null, "whom": 1, "choice_afl": "F" }, "model": "blog.notification", "pk": 3 }, { "fields": { "how_much_new_notifications": 0, "number_of_new_notifications": 0, "who_did": 2, "question_answered_object": null, "whom": 1, "choice_afl": "F" }, "model": "blog.notification", "pk": 4 }, { "fields": { "how_much_new_notifications": 0, "number_of_new_notifications": 0, "who_did": 2, "question_answered_object": null, "whom": 1, "choice_afl": "F" }, "model": "blog.notification", "pk": 5 }, { "fields": { "how_much_new_notifications": 0, "number_of_new_notifications": 0, "who_did": 2, "question_answered_object": null, "whom": 1, "choice_afl": "F" }, "model": "blog.notification", "pk": 6 } ]
我认为错误出在你的JS代码中。尝试按如下方式重写循环:
当前
acd
只是数组的索引,而不是它的元素(一个很好的解释here)。在相关问题 更多 >
编程相关推荐