Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting PATCH request parameter in Django

Is it possible to retrieve the request parameters of a HTTP PATCH request in Django? request.method == 'PATCH' is recognised, but I struggle to retrieve the request payload. I've tried request.REQUEST.items(), but this didn't contain any data. I know I could use Django-tastypie, but in this case, I would like to avoid it (and I supposed tastypie is using some Django methods to retrieve this data anyway).

I'm using Django 1.5.1.

like image 222
orange Avatar asked Sep 26 '13 04:09

orange


People also ask

How do you send a patch request in Python?

To send a PATCH request to the server, you need to use the HTTP PATCH method and include the request data in the body of the HTTP message. The Content-Type request header must indicate the data type in the body. In this PATCH request example, we send JSON to the ReqBin echo endpoint to update the data on the server.

What is request method == post in Django?

The result of request. method == "POST" is a boolean value - True if the current request from a user was performed using the HTTP "POST" method, of False otherwise (usually that means HTTP "GET", but there are also other methods).

What is get request in Django?

Django uses request and response objects to pass state through the system. When a page is requested, Django creates an HttpRequest object that contains metadata about the request. Then Django loads the appropriate view, passing the HttpRequest as the first argument to the view function.

What is request Meta in Django?

META contains all the metadata of the HTTP request that is coming to your Django server, it can contain the user agent, ip address, content type, and so on.


1 Answers

You can use a QueryDict class manually. This is class implemented in django and processing all text data received through http request.

Link on the docs: https://docs.djangoproject.com/en/1.11/ref/request-response/#django.http.QueryDict

And here is an example of usage:

from django.http import QueryDict

def home_view(request):

    if request.method == 'PATCH':
        data = QueryDict(request.body)
        print data['your_field']
like image 148
Denti Avatar answered Oct 03 '22 08:10

Denti