Django - Where are the params stored on a PUT/DELETE request?
Solution 1:
I am using django v1.5. And I mainly use QueryDict to solve the problem:
from django.http import QueryDict
put = QueryDict(request.body)
description = put.get('description')
and in *.coffee
$.ajax
url: "/policy/#{policyId}/description/"
type: "PUT"
data:
description: value
success: (data) ->
alert data.body
fail: (data) ->
alert "fail"
You can go here to find more information. And I hope this can help you. Good luck:)
Solution 2:
I assume what you're asking is if you can have a method like this:
def restaction(request, id):
if request.method == "PUT":
someparam = request.PUT["somekey"]
The answer is no, you can't. Django doesn't construct such dictionaries for PUT, OPTIONS and DELETE requests, the reasoning being explained here.
To summarise it for you, the concept of REST is that the data you exchange can be much more complicated than a simple map of keys to values. For example, PUTting an image, or using json. A framework can't know the many ways you might want to send data, so it does the obvious thing - let's you handle that bit. See also the answer to this question where the same response is given.
Now, where do you find the data? Well, according to the docs, django 1.2 features request.raw_post_data
. As a heads up, it looks like django 1.3 will support request.read()
i.e. file-like semantics.
Solution 3:
Ninefiger's answer is correct. There are, however, workarounds for that.
If you're writing a REST style API for a Django project, I strongly suggest you use tastypie. You will save yourself tons of time and guarantee a more structured form to your API. You can also look at how tastypie does it (access the PUT and DELETE data).
Solution 4:
There was a problem that I couldn't solve how to parse multipart/form-data from request
. QueryDict(request.body)
did not help me.
So, I've found a solution for me. I started using this:
from django.http.multipartparser import MultiPartParser
You can get data from request
like:
MultiPartParser(request.META, request, request.upload_handlers).parse()