Next,I'mhavingtroublewiththismessage..whatcanitbe?
Followmycode!!Iamtryingtoaccessthe"page" and this message appears ..
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. It actually has an if:
if request.method == 'POST':
If this is false, you will not even get into that part of the code you posted. Remember that if the function does not execute any return
python automatically returns None
which may be causing its error.
This is because Python picks up the block by indentation.
Note that your return render
is "on the same vertical line" as if request.method == 'POST'
.
This will cause your add_user
method to only return render
or HttpResponse
if the method is POST
In addition there is another snippet that seems to be an error, which is else
. It seems to me that the intention was to leave form = UserForm()
empty only if it was not a POST request.
The closest 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 case of your validation if the form is valid, it does not make sense not to have a else
, so 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})