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

What does request.method == "POST" mean 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).

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 QueryDictelse:    # 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 renderfrom django.http import HttpResponseRedirectdef 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 1elif request.method == "POST":    # functionality 2elif request.method == "PUT":    # functionality 3elif 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.


book_id = Book.objects.get(id=id)if request.method == 'POST':

    book_save == BookForm(request.POST, request.FILES, instance=book_id)    if book_save.is_valid():        book_save.save()else:    book_save = BookForm(instance=book_id)y = {    'form':book_save,}return render(request, 'pages/update.html', y)