Httpresponse not returning anything

Asked

Viewed 124 times

0

inserir a descrição da imagem aqui Next, I’m having trouble with that message.. Which could be?

Follow my code!! I’m trying to access "page" and this message comes up..

inserir a descrição da imagem aqui

  • 1

    Welcome, first edit your question by pasting the code, not the print. The way the question is we can hardly help

  • Thank you! I am without the source code at the moment as I am not on my private computer. As soon as possible, I update the post.

2 answers

0


The image does not show the whole code, but it looks like your view is returning None.

You have marked a line of code, but this is not the only line that returns something in this method. In fact you have an if:

if request.method == 'POST':

If this is false, you will not even enter this part of the code you posted. Remember that if the function does not perform any return python automatically returns None what may be causing your error.

0

It’s because Python takes the block by indentation.

Note that your return render is "on the same vertical line" as the if request.method == 'POST'.

This will make your method add_user only return the render or HttpResponse if the method is POST

In addition there is another passage that seems to be a mistake, which is the else. It seems to me the intention was to leave the form = UserForm() empty only if it wasn’t a POST request.

the most approximate correction in your case is the following code:

def add_user(request):

    if request.method == 'POST':
        form = UserForm(request.POST)

        if form.is_valid():
            form.save()
            return HttpResponse("Usuário criado com sucesso")
    else:
        form = UserForm()


    return render(request, 'accounts/add_user.html', {'form' : form})

In the case of its validation if the form is valid, it makes no sense not to have a else, then I would add one more:

def add_user(request):

    if request.method == 'POST':
        form = UserForm(request.POST)

        if form.is_valid():
            form.save()
            return HttpResponse("Usuário criado com sucesso")
        else:
            # Redirecione ou mostre uma mensagem de erro aqui
    else:
        form = UserForm()


    return render(request, 'accounts/add_user.html', {'form' : form})
  • Could you leave feedback on the negative? What’s wrong with the answer?

  • I performed a new interaction, could help me?

  • @duardoalmeida if it’s new information, you can ask a new question on the site

  • is referring to the same question, because I haven’t been able to solve the problem yet.. the interaction I did was in this post

  • What new interaction?

Browser other questions tagged

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