我正在使用DJango 1.8和Python 3.4
当运行以下视图时,Django抛出类型错误 - 对象不是JSON Serializable
Views.py
from django.http import HttpRequest,HttpResponse
from django.http import JsonResponse
from json import dumps
def get_stats(request):
if request.method == "POST":
srch_dropV = request.POST['srch_dropAJ']
else:
srch_dropV = ''
if(srch_dropV == 'Green'):
students = GreenBased.objects.all()
if(srch_dropV == 'Yellow'):
students = YellowBased.objects.all()
response_data = {}
try:
response_data['result'] = 'Success'
response_data['message'] = list(students)
except:
response_data['result'] = 'Ouch!'
response_data['message'] = 'Script has not ran correctly'
return HttpResponse(JsonResponse(response_data), content_type="application/json")
Run Code Online (Sandbox Code Playgroud)
我正在尝试从mysql数据库中读取几行并将其显示在html文件中,当我正在运行上面的视图时,我正面临下面的错误消息
TypeError: YellowBased: YelloBased object is not JSON serializable
Run Code Online (Sandbox Code Playgroud)
在HTML页面中,我有一个下拉列表..基于所选的选项,我的Ajax会返回从mysql表中获取的记录.
Models.py …