为什么Django在POST之后触发GET?
Why does Django trigger GET after POST?
我有以下视图,它采用 URL 或上传的文本文件,创建词云,最后将生成的图像显示给用户。
def create(request):
"""
Displays the generated WordCloud from the
given URI or uploaded file
"""
response = HttpResponse(content_type="image/png")
# in order to avoid KeyError
myfile = request.FILES.get('myfile', None)
if request.POST['uri'] == '' and myfile is None:
return render(request, 'nube/index.html', {
'error_message': NOTHING_TO_PROCESS
})
try:
if myfile:
cloud = WordCloud(myfile, type="upload")
else:
cloud = WordCloud(request.POST['uri'], type="internet")
except (MissingSchema):
return render(request, 'nube/index.html', {
'error_message': URI_COULD_NOT_BE_PROCESSED
})
else:
# img is a PIL.Image instance
img = cloud.get_word_cloud_as_image()
img.save(response, 'PNG')
return response
图片显示没有问题; POST 请求被正确处理,从日志中可以看出:
[16/Jan/2018 22:53:25] "POST /nube/create HTTP/1.1" 200 216961
然而,即使服务器没有崩溃,我也注意到每次紧随其后都会引发异常:
Internal Server Error: /nube/create
Traceback (most recent call last):
File "C:\repos\phuyu\venv\lib\site-packages\django\utils\datastructures.py", line 77, in __getitem__
list_ = super().__getitem__(key)
KeyError: 'uri'
调试代码后,我注意到我的 create
视图再次被调用,但这次是作为 GET 请求,当然还有参数 uri
和 myfile
这次不存在,因此引发异常。
为了确保,我将 create
更改为基于 class 的视图,并且只定义了它的 post
方法。正如所怀疑的那样,现在我在成功 POST:
之后在日志中得到了以下行
Method Not Allowed (GET): /nube/create
[16/Jan/2018 22:44:41] "GET /nube/create HTTP/1.1" 405 0
处理这个问题的正确方法是什么?我是 Django 的新手。
正如@usman-maqbool 所建议的,问题实际上出在我的 WordCloud 代码中,特别是在 get_word_cloud_as_image()
中,一个尾随分号:
def get_word_cloud_as_image(self):
return self.img;
删除后,就不会再有偷偷摸摸的 GET 请求了。我不确定为什么会产生这种效果。如果有人可以澄清,我将不胜感激。
我有以下视图,它采用 URL 或上传的文本文件,创建词云,最后将生成的图像显示给用户。
def create(request):
"""
Displays the generated WordCloud from the
given URI or uploaded file
"""
response = HttpResponse(content_type="image/png")
# in order to avoid KeyError
myfile = request.FILES.get('myfile', None)
if request.POST['uri'] == '' and myfile is None:
return render(request, 'nube/index.html', {
'error_message': NOTHING_TO_PROCESS
})
try:
if myfile:
cloud = WordCloud(myfile, type="upload")
else:
cloud = WordCloud(request.POST['uri'], type="internet")
except (MissingSchema):
return render(request, 'nube/index.html', {
'error_message': URI_COULD_NOT_BE_PROCESSED
})
else:
# img is a PIL.Image instance
img = cloud.get_word_cloud_as_image()
img.save(response, 'PNG')
return response
图片显示没有问题; POST 请求被正确处理,从日志中可以看出:
[16/Jan/2018 22:53:25] "POST /nube/create HTTP/1.1" 200 216961
然而,即使服务器没有崩溃,我也注意到每次紧随其后都会引发异常:
Internal Server Error: /nube/create
Traceback (most recent call last):
File "C:\repos\phuyu\venv\lib\site-packages\django\utils\datastructures.py", line 77, in __getitem__
list_ = super().__getitem__(key)
KeyError: 'uri'
调试代码后,我注意到我的 create
视图再次被调用,但这次是作为 GET 请求,当然还有参数 uri
和 myfile
这次不存在,因此引发异常。
为了确保,我将 create
更改为基于 class 的视图,并且只定义了它的 post
方法。正如所怀疑的那样,现在我在成功 POST:
Method Not Allowed (GET): /nube/create
[16/Jan/2018 22:44:41] "GET /nube/create HTTP/1.1" 405 0
处理这个问题的正确方法是什么?我是 Django 的新手。
正如@usman-maqbool 所建议的,问题实际上出在我的 WordCloud 代码中,特别是在 get_word_cloud_as_image()
中,一个尾随分号:
def get_word_cloud_as_image(self):
return self.img;
删除后,就不会再有偷偷摸摸的 GET 请求了。我不确定为什么会产生这种效果。如果有人可以澄清,我将不胜感激。