def post(request): or request.method == POST?

Asked

Viewed 295 times

0

I’m doubting the title above about the best development practices in Django/python.

Example:

class Exemplo(View):

    def get(self, request):
        pass

    def post(self, request):
        pass

OR

if request.method == 'GET':
    do_something()

elif request.method == 'POST':
    do_something_else()
  • What do you mean? The two expressions do very different things. You could better contextualize the doubt?

  • added examples. @Andersoncarloswoss

  • Have you studied about the principle of single responsibility?

  • @Andersoncarloswoss I have seen some articles about, my doubt is about Django, the post and get process are always managed in the same view, no matter the article or design Pattern adopted, so my doubt!

  • "are always managed in the same view", where you read this?

1 answer

0

Short answer: neither of the two!

Long answer:

Each web framework has a different way of dealing with different types of requests, but in this case we are talking about the Django. Django has several ways to handle requests, and each has a specific use case.

There is not a single definitive form considered best practice - each form has its reason to exist and its specific case where it should be used.

The question is wide, and the added example has a generic scope that adds nothing about the context of the code. The generic response in the case of Django is to do as suggested in official tutorial of Django.

In this tutorial, a form is created that uses the POST method, and the view verification takes place within a Django method:

selected_choice = question.choice_set.get(pk=request.POST['choice'])

As you can see, the code simply attempts to access the form data through the request.POST, and if this data is there, it is assumed that the form was sent via POST.

It is a very efficient way, because with only one command line, it is verified that the form was sent through POST and with the correct variable filled, and that the selected option really exists. Much better than doing separate checks for each of these possible failures.

Therefore, the form generic to do the verification by POST is not make her, according to the official tutorial.

There are specific ways to verify this, but should be used in specific cases and how best to do it depends on the details in each use case.

  • Nosklo, I will contextualize a little more I got to see the official documentation in this part, but I come across the question of (validation)security, usually I do the security (validation) using both html, as in the view and model before going to the database, as in the documentation suggests the basic protections, so the context is the following: I have an html "in hand" with the validations by jquery and javascript and my form in Wizard style 2 years ago adopted to use the first option I provided you with get() and post()but every article I read there is a variation.

  • @Rodrigocarvalho The question remains half obscure, the answer is the same: There is no a standardized way of checking because the form recommended by the official documentation is not make the check; That’s why you see different articles with different shapes. Each author must have had a reason to use the form they used.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.