所以我最近迁移到了python 3.6和django 1.11,我的jsonresponse代码如下所示:

   return JsonResponse({'status': '1'})

它工作得很好,但是在迁移之后,我开始发现这个错误:
typeerror:“bytes”类型的对象不是json可序列化的
在打印了传递给JsonResponse的数据类型之后,我意识到python 3.6将其从dict改为byte。
所以我修改了代码以确保我传递的是一个dict。
在尝试了所有这些之后,我仍然会遇到同样的错误:
    data = dict([('status', 0)])
    print(data)
    print(type(data))
    # print(type(json.dumps(data)))
    # data = {"status": '0'}
    # data = json.dumps(data)
    # json.dumps(data.decode("utf-8"))
    #response = json.JSONEncoder().encode({"status": 0})
    #JsonResponse(data, safe=False)
    # response = json.dumps(data)
    print(JsonResponse(data, safe=False))
    return JsonResponse(data, safe=False)

印刷品:
    {'status': 0}
    <class 'dict'>
    <JsonResponse status_code=200, "application/json">

使用json.dumps选项,可以改为获取此错误
attributeerror:“str”对象没有“get”属性
任何帮助都将不胜感激
回溯
    Traceback (most recent call last):
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/core/handlers/base.py", line 131, in get_response
    response = middleware_method(request, response)
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/middleware.py", line 58, in process_response
request.session.save()
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py", line 81, in save
return self.create()
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py", line 54, in create
self.save(must_create=True)
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py", line 83, in save
obj = self.create_model_instance(data)
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/backends/db.py", line 69, in create_model_instance
session_data=self.encode(data),
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/contrib/sessions/backends/base.py", line 98, in encode
serialized = self.serializer().dumps(session_dict)
    File "/Users/andresvillavicencio/bancompara.mx/lib/python3.6/site-packages/django/core/signing.py", line 93, in dumps
return json.dumps(obj, separators=(',', ':')).encode('latin-1')
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/__init__.py", line 238, in dumps
**kw).encode(obj)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
    File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/json/encoder.py", line 180, in default
o.__class__.__name__)
   TypeError: Object of type 'bytes' is not JSON serializable

最佳答案

问题不在于return JsonResponse({'status': '1'})
回溯显示当Django试图保存Django会话时发生错误。
你一定是在做这样的事情:

request.session['my_key'] = b'bytes'

对于该示例,您必须解码bytes对象(或使用字符串代替):
request.session['my_key'] = b'bytes'.decode('utf-8')

10-08 12:58