31

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"?

ivanleoncz
  • 5,662
  • 3
  • 49
  • 42
  • 3
    possible duplicate of [What is the difference between POST and GET?](http://stackoverflow.com/questions/3477333/what-is-the-difference-between-post-and-get) – Alasdair Oct 02 '13 at 07:57
  • 1
    i dont know why i got downvote. I really want to know this –  Oct 02 '13 at 07:58
  • 2
    I didn't downvote you, but there are lots of questions on Stack Overflow that explain the difference between GET and POST requests. Once you understand that, then you can see that the if statement separates your view into two branches, one for GET requests (e.g. to view a form) and the other for POST requests (e.g. when the form is submitted). – Alasdair Oct 02 '13 at 08:02
  • thank you very much for the link and explanation :) –  Oct 02 '13 at 08:06

3 Answers3

30

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,
    })
Community
  • 1
  • 1
Ludwik Trammer
  • 21,961
  • 6
  • 61
  • 86
  • How to check there is data in the POST? I mean, `if request.method=='POST' and if request.POST` When will that be true, When we get POST and we have some data in the post? – A.J. Mar 10 '14 at 10:43
14

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.

anjaneyulubatta505
  • 7,742
  • 1
  • 32
  • 50
  • 1
    The 1.11 documentation makes an alternate recommendation: "It’s possible that a request can come in via POST with an empty POST dictionary – if, say, a form is requested via the POST HTTP method but does not include form data. Therefore, you shouldn’t use `if request.POST` to check for use of the POST method; instead, use if `request.method == "POST"`." – GSP Jun 19 '17 at 17:57
  • 1
    and the source of that doc: https://docs.djangoproject.com/en/1.11/ref/request-response/#django.http.HttpRequest.POST – Marshall X Nov 13 '17 at 18:47
-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)
iraq
  • 1