Reputation: 27861
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?
Upvotes: 2
Views: 1890
Reputation: 17229
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:
$.ajax({
type: "POST",
url: "/something",
});
Upvotes: 0
Reputation: 90782
TL;DR: request.method
is never None
in real use, but for your particular case, you're looking at the wrong thing.
HttpRequest
django/http/__init__.py
:
class HttpRequest(object):
...
def __init__(self):
...
self.method = None
...
When a plain HttpRequest
is instantiated, its method is None
. But then, WSGIRequest
and ModPythonRequest
don't call HttpRequest.__init__
ever.
mod_wsgi
django/core/handlers/wsgi.py
:
class WSGIRequest(http.HttpRequest):
...
def __init__(self, environ):
...
self.method = environ['REQUEST_METHOD'].upper()
...
The summary of this is that for mod_wsgi, request.method
will never be None
. If in some warped way you managed to get environ['REQUEST_METHOD']
not being defined or being None
, the request would fail.
mod_python
django/core/handlers/modpython.py
:
class ModPythonRequest(http.HttpRequest):
...
def _get_method(self):
return self.META['REQUEST_METHOD'].upper()
...
method = property(_get_method)
Same remarks as with WSGIRequest
apply. Can't ever be None
.
django.test.client.RequestFactory.request
instantiates a WSGIRequest
and is called every time with REQUEST_METHOD
defined in the environ
as an uppercase string, as it should be.
Summary:
assert request.method is not None
You're looking for the error in the wrong place. In this case, request.method == 'POST'
. It's failing when request.META.get('CONTENT_TYPE', '') is None
. The reason for this is that the Content-Type
header isn't being sent by the client in the request (don't ask me why, I'm not familiar with that stuff).
Upvotes: 7