Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does request.method == "POST" mean in Django?

People also ask

What does POST do in Django?

looks strange at first but when the user validates the form, the same page is loaded but the POST data is processed in the django form for data validation (like checking if a required field was left blank, etc…) in order to display errors in the form. If there is no error (form.

What are request get and request POST objects in Django?

In an HttpRequest object, the GET and POST attributes are instances of django. http. QueryDict , a dictionary-like class customized to deal with multiple values for the same key. This is necessary because some HTML form elements, notably <select multiple> , pass multiple values for the same key.

How does Django define request?

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.


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).

You can read more about difference between GET and POST in answers to the question Alasadir pointed you to. In a nutshell POST requests are usually used for form submissions - they are required if processing a form would change server-side state (for example add user to a database, in case of a registration form). GET is used for normal HTTP requests (for example when you just type an URL into your browser) and for forms that can be processed without any side-effects (for example a search form).

The code is usually used in conditional statements, to distinguish between code for processing a submitted form, and code for displaying an unbound form:

if request.method == "POST":
    # HTTP Method POST. That means the form was submitted by a user
    # and we can find her filled out answers using the request.POST QueryDict
else:
    # Normal GET Request (most likely).
    # We should probably display the form, so it can be filled
    # out by the user and submitted. 

And here is another example, taken straight from Django documentation, using Django Forms library:

from django.shortcuts import render
from django.http import HttpResponseRedirect

def contact(request):
    if request.method == 'POST': # If the form has been submitted...
        form = ContactForm(request.POST) # A form bound to the POST data
        if form.is_valid(): # All validation rules pass
            # Process the data in form.cleaned_data
            # ...
            return HttpResponseRedirect('/thanks/') # Redirect after POST
    else:
        form = ContactForm() # An unbound form

    return render(request, 'contact.html', {
        'form': form,
    })

request.methodreturns the type of the request method it may be GET,POST,PUT,DELETE etc. after returning you are comparing it with your string. comparison operator always provides a boolean value(True or False).

Some times we need to handle the functionality based on the requested method type.

if request.method == "GET":
    # functionality 1
elif request.method == "POST":
    # functionality 2
elif request.method == "PUT":
    # functionality 3
elif request.method == "DELETE":
    # functionality 4

for request method GET data is passed along with url. for request method POST data is passed inside body. In terms of security method type POST is better one.