Django 中如何 request.method == None
我试图从 https 找出与 Django 相关的问题://github.com/miki725/Django-jQuery-File-Uploader-Integration-demo/issues/1
在什么条件下Django中可以request.method == None
?
I am trying to figure out this issue related to Django from https://github.com/miki725/Django-jQuery-File-Uploader-Integration-demo/issues/1
Under what conditions can request.method == None
in Django?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
TL;DR:
request.method
在实际使用中永远不会None
,但对于您的特定情况,您正在寻找错误的东西。通用
HttpRequest
django/http/__init__.py
:当实例化普通
HttpRequest
时,其方法为None
。但是,WSGIRequest
和ModPythonRequest
永远不会调用HttpRequest.__init__
。通过
mod_wsgi
请求django/core/handlers/wsgi.py
:总结来说,对于 mod_wsgi,
request.method
将 永远不会无
。如果您以某种扭曲的方式设法使environ['REQUEST_METHOD']
未定义或为None
,则请求将失败。通过
mod_python
的请求django/core/handlers/modpython.py
:与
WSGIRequest
的注释相同。永远不可能是None
。测试客户端
django.test.client.RequestFactory.request
实例化一个WSGIRequest
并每次使用environ< 中定义的
REQUEST_METHOD
进行调用/code> 作为大写字符串,正如它应该的那样。摘要:
您在错误的位置查找错误。在本例中,
request.method == 'POST'
。当request.META.get('CONTENT_TYPE', '') 为 None
时失败。原因是客户端没有在请求中发送Content-Type
标头(不要问我为什么,我对这些东西不熟悉)。TL;DR:
request.method
is neverNone
in real use, but for your particular case, you're looking at the wrong thing.Generic
HttpRequest
django/http/__init__.py
:When a plain
HttpRequest
is instantiated, its method isNone
. But then,WSGIRequest
andModPythonRequest
don't callHttpRequest.__init__
ever.Requests through
mod_wsgi
django/core/handlers/wsgi.py
:The summary of this is that for mod_wsgi,
request.method
will never beNone
. If in some warped way you managed to getenviron['REQUEST_METHOD']
not being defined or beingNone
, the request would fail.Requests through
mod_python
django/core/handlers/modpython.py
:Same remarks as with
WSGIRequest
apply. Can't ever beNone
.Test client
django.test.client.RequestFactory.request
instantiates aWSGIRequest
and is called every time withREQUEST_METHOD
defined in theenviron
as an uppercase string, as it should be.Summary:
You're looking for the error in the wrong place. In this case,
request.method == 'POST'
. It's failing whenrequest.META.get('CONTENT_TYPE', '') is None
. The reason for this is that theContent-Type
header isn't being sent by the client in the request (don't ask me why, I'm not familiar with that stuff).我能够通过 AJAX 实现这一点,特别是调用 jQuery 的 $.ajax 函数(请参阅 http:// api.jquery.com/jQuery.ajax/) 具有“POST”类型但没有数据:
I was able to get this to happen via AJAX, specifically calling jQuery's $.ajax function (see http://api.jquery.com/jQuery.ajax/) with "POST" type but no data: