如何将对象列表转储到 python django 中的 json
How to dumps list of object in to json in python django
这里我试图将 json
传递给 javascript
。但它 returns 空列表。
我需要将我的数据列表转储到 json format.but json 是 empty.please 帮助我
这是我的代码
views.py
from django.utils import simplejson
def student_search(request):
location = rg('location')
results = StudentProfile.objects.filter(Q(address_line1__icontains=location) \
| Q(address_line2__icontains=location)).values()
for result in results:
students_list.append(result)
if request.is_ajax():
result = {
'student': [student._json() for student in students_list]
}
return HttpResponse(
simplejson.dumps(result), content_type="application/json",
)
return render_to_response("account/students.html",{'students_list' : students_list }, context_instance= RequestContext(request))
urls.py
url(r'student_search', 'account.views.student_search', name='student_searchview'),
模板
<div class="hold"></div>
<input type="button" id="btnLoadMore" value="Load More"/>
<script type="text/javascript">
$(document).ready(function () {
$.get("/student_search/", function (data) {
alert ("enter");
alert(JSON.stringify(data));
});
});
</script>
99% 的问题来自
results = StudentProfile.objects.filter(Q(address_line1__icontains=location) \
| Q(address_line2__icontains=location)).values()
你绝对确定这个returns列表bool
、float
、int
、string
、list
或 dict
?
这里我试图将 json
传递给 javascript
。但它 returns 空列表。
我需要将我的数据列表转储到 json format.but json 是 empty.please 帮助我
这是我的代码 views.py
from django.utils import simplejson
def student_search(request):
location = rg('location')
results = StudentProfile.objects.filter(Q(address_line1__icontains=location) \
| Q(address_line2__icontains=location)).values()
for result in results:
students_list.append(result)
if request.is_ajax():
result = {
'student': [student._json() for student in students_list]
}
return HttpResponse(
simplejson.dumps(result), content_type="application/json",
)
return render_to_response("account/students.html",{'students_list' : students_list }, context_instance= RequestContext(request))
urls.py
url(r'student_search', 'account.views.student_search', name='student_searchview'),
模板
<div class="hold"></div>
<input type="button" id="btnLoadMore" value="Load More"/>
<script type="text/javascript">
$(document).ready(function () {
$.get("/student_search/", function (data) {
alert ("enter");
alert(JSON.stringify(data));
});
});
</script>
99% 的问题来自
results = StudentProfile.objects.filter(Q(address_line1__icontains=location) \
| Q(address_line2__icontains=location)).values()
你绝对确定这个returns列表bool
、float
、int
、string
、list
或 dict
?