Python 3.x 如何修复';这个QueryDict实例是不可变的';通过urllib.request发送http post时
我正在通过urllib.request编写发送方,但在服务器中获取“This QueryDict instance is immutable” 当我在服务器代码(django-tastypie)中添加“bundle.request.POST.\u mutable=True”时,这一问题得到了修复 但我想知道如何解决寄件人的问题 python:v3.6 发件人:urllib.request 服务器:django tastypie 发件人:Python 3.x 如何修复';这个QueryDict实例是不可变的';通过urllib.request发送http post时,python-3.x,urllib,Python 3.x,Urllib,我正在通过urllib.request编写发送方,但在服务器中获取“This QueryDict instance is immutable” 当我在服务器代码(django-tastypie)中添加“bundle.request.POST.\u mutable=True”时,这一问题得到了修复 但我想知道如何解决寄件人的问题 python:v3.6 发件人:urllib.request 服务器:django tastypie 发件人: cookie = http.cookiejar.Mo
cookie = http.cookiejar.MozillaCookieJar()
handler = urllib.request.HTTPCookieProcessor(cookie)
opener = urllib.request.build_opener(handler)
request = urllib.request.Request(url,
data=data,headers=headers,method=method)
rep = opener.open(request)
服务器中的错误:
File "/python3.6/site-packages/tastypie/resources.py", line 1408, in post_list
updated_bundle = self.obj_create(bundle, **self.remove_api_resource_names(kwargs))
File "/python3.6/site-packages/tastypie/resources.py", line 2244, in obj_create
bundle = self.full_hydrate(bundle)
File "/python3.6/site-packages/tastypie/resources.py", line 943, in full_hydrate
bundle = self.hydrate(bundle)
File "/home/ma/*/api.py", line 115, in hydrate
bundle.data["create_by"] = user
File "/python3.6/site-packages/django/http/request.py", line 421, in __setitem__
self._assert_mutable()
File "/python3.6/site-packages/django/http/request.py", line 418, in _assert_mutable
raise AttributeError("This QueryDict instance is immutable")
我找到了一个原因,application/x-www-form-urlencoded->application/json。我找到了一个原因,application/x-www-form-urlencoded->application/json。对于那些在从测试用例发送有效负载时请求查询信息类型,但在从前端传递数据(例如React)时得到Dict的人来说-您可以通过将负载作为JSON传递来避免处理Querydict,因此,您的request.data将是“dict”类型 示例
payload = {
"name": "Dict example"
}
response = self.client.post(ENDPOINT, json.dumps(chat_payload),content_type="application/json"
)
通过这种方式,您可以像普通dict一样访问request.data[“name”],并使操作更简单。对于那些在请求中获得Querydict类型的人,当从测试用例发送有效负载时,但是当从前端传递数据时(例如React)使用dict-您可以通过将负载作为JSON传递来避免处理Querydict,因此,您的request.data将是“dict”类型 示例
payload = {
"name": "Dict example"
}
response = self.client.post(ENDPOINT, json.dumps(chat_payload),content_type="application/json"
)
这样,您就可以像普通dict一样访问request.data[“name”],从而简化操作