问题
The following code works fine under Django 1.2 but fails under Django 1.3 because request.POST is empty, although the form data is posted correctly to the server
def commit_form(request):
logging.debug(str(datetime.datetime.now()) + ": commit data " + request.raw_post_data)
purchase = Purchase.objects.get(transaction_id=request.POST['TransactionID']) #breaks here, request.POST is empty
#other stuff...
return HttpResponse("GOOD", mimetype="text/plain")
Why does this not work, I can't see any error here?
回答1:
I recall having a problem of this kind, it turned out that accessing request.raw_post_data
prevented the request.POST
dict from being subsequently populated with the post params. What happens if you remove the logging.debug line?
回答2:
I had a similar problem (empty request.POST) with django 1.3, but it was due to a bug in my HTML.
I had a form with action="foo"
but my urls.py mapped "foo/" to the view (where I was expecting some POST data, and getting none). Django kindly server-side redirects requests for "foo" to "foo/", but the POST data was not being resubmitted to "foo/" as part of the deal. In hindsight that seems quite sensible. Changing my form (to action="foo/"
) fixed it, I suppose changing the urls.py could have worked too.
回答3:
Also check your content type header.
Eg.
'CONTENT_TYPE': 'application/x-www-form-urlencoded'
Earlier versions were more accommodating if this header was missing or invalid.
来源:https://stackoverflow.com/questions/7443547/empty-request-post-after-upgrade-to-django-1-3