I'd like to follow the RESTful pattern for my new django project, and I'd like to know where the parameters are when a PUT/DELETE request is made.
As far as I know, I only see GET & POST QueryDict in the request, no others.
Is Django adding a new PUT or DELETE QueryDict regarding to the request, or does it add the parameters to GET or POST QueryDict ?
Thanks for your help.
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:)
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.
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).
You can see an example of getting a QueryDict for a PUT method in django-piston's code (See the coerce_put_post method)
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()