Django - Where are the params stored on a PUT/DELETE request?

前端 未结 7 1164
[愿得一人]
[愿得一人] 2020-11-30 00:17

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

7条回答
  •  [愿得一人]
    2020-11-30 00:56

    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.

提交回复
热议问题