小编use*_*771的帖子

TypeError:对象在DJango 1.8 Python 3.4中不是JSON可序列化的

我正在使用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 …

python django serialization json python-3.x

2
推荐指数
1
解决办法
2万
查看次数

标签 统计

django ×1

json ×1

python ×1

python-3.x ×1

serialization ×1