Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - request POST

Shall I use (and why?):

if request.POST

or:

if request.method == 'POST'

Is there any differences except syntax?

like image 802
Mateusz Jagiełło Avatar asked May 25 '12 18:05

Mateusz Jagiełło


People also ask

What is request POST return Django?

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. Each view is responsible for returning an HttpResponse object.

What is post method in Django?

Django's login form is returned using the POST method, in which the browser bundles up the form data, encodes it for transmission, sends it to the server, and then receives back its response. GET , by contrast, bundles the submitted data into a string, and uses this to compose a URL.

How do I send a post request in Python?

To send a POST request using the Python Requests Library, you should call the requests. post() method and pass the target URL as the first parameter and the POST data with the data= parameter.


1 Answers

If you want to check the request method, use if request.method == 'POST'.

request.POST is the post param dict, and you shouldn't count on its existence or lack thereof when it comes to the request method. (e.g. a post request with no params fails on that test.)

Explicit is better than implicit. -- PEP 20, Zen of Python

like image 169
Yuval Adam Avatar answered Sep 22 '22 12:09

Yuval Adam