I have recently found that in our team, both

if request.POST:
    pass

and

 if request.method == POST:
    pass

have been used to check a POST request. Are they the same? Which one is better? I have googled around and find out that they are different!

  • request.method returns a string of the method used with the request, and nothing else. This is important because you can use HTTP verbs without sending data.

  • If you do a boolean check as request.POST, it checks to make sure that there is data in the POST QueryDict dictionary. If there is data then it was a POST; if no data then it evaluates as false as if no POST happened.

Thus, request.method == ‘POST’ is recommended as we are expecting data from a POST request.