gpt4 book ai didi

python 3.6 JsonResponse 问题

转载 作者:太空宇宙 更新时间:2023-11-03 15:50:39 25 4
gpt4 key购买 nike

所以我最近迁移到了 Python 3.6 和 Django 1.11,我的 JsonResponse 代码如下所示:

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

它工作正常,但在迁移后我开始收到此错误:

TypeError: Object of type 'bytes' is not JSON serializable

打印传递给 JsonResponse 的数据类型后,我意识到 python 3.6 将其从 dict 更改为 byte。所以我更改了代码以确保我传递的是字典。

尝试了所有这些之后,我仍然遇到同样的错误:

    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' object has no attribute '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 session 时。

你必须在 View 中做这样的事情:

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

对于该示例,您必须解码字节对象(或改用字符串):

request.session['my_key'] = b'bytes'.decode('utf-8')

关于python 3.6 JsonResponse 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47042998/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com