user2657599
user2657599

Reputation:

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

I am using this thing in my views quite a lot but I want to know what exactly does that mean.

What happens when we write request.method == "GET" or request.method == "POST"?

Upvotes: 39

Views: 97328

Answers (3)

iraq
iraq

Reputation: 1

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)

Upvotes: -1

anjaneyulubatta505
anjaneyulubatta505

Reputation: 11695

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.

Upvotes: 15

Ludwik Trammer
Ludwik Trammer

Reputation: 25052

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,
    })

Upvotes: 33

Related Questions