Tuesday, October 31, 2017

Re: do i always have to reload/return render a page to show the errors? if i press go back on browser, i see screens with the error message



On Oct 30, 2017 4:46 PM, "fábio andrews rocha marques" <fabioandrewsrochamarques@gmail.com> wrote:
Let's say i have a "register a new user" View and on this page, when the user forgets to inform a password or a username, i show a message to him on the same page saying "you forgot the password". The way i do this is by doing this(on my View.py in a function called cadastrarprofessor):

nomeusuario = request.POST['usuario'] #username
nomesenha = request.POST['senha'] #password
nomeemail = request.POST['email'] #email

if not nomeusuario:
return render(request,'cadastro.html',{'cadastrorealizadocomsucesso': False, 'error_message': "informe um usuário", 'nomeemailcadastro':nomeemail, 'nomesenhacadastro':nomesenha})
elif not nomesenha:
return render(request,'cadastro.html',{'cadastrorealizadocomsucesso': False, 'error_message': "informe uma senha", 'nomeemailcadastro':nomeemail, 'nomeusuariocadastro':nomeusuario})
elif not nomeemail:
return render(request,'cadastro.html',{'cadastrorealizadocomsucesso': False, 'error_message': "informe um email", 'nomesenhacadastro':nomesenha, 'nomeusuariocadastro':nomeusuario})

And my template for this page(cadastro.html) is like this:
<meta charset="utf-8"/>
<h1>Cadastro</h1>
<form action="{% url 'cadastrarprofessor' %}" method="post">
{% csrf_token %}
<label for="usuario">Usuário: </label>
{% if nomeusuariocadastro %}
<input id="usuario" type="text" name="usuario" value={{ nomeusuariocadastro }}>
{% else %}
<input id="usuario" type="text" name="usuario" value=""/>
{% endif %}


... (DO THE SAME TO SENHA/PASSWORD AND EMAIL)

<input type="submit" value="Cadastrar"/>
</form>
<form action="{% url 'index' %}" method="post">
<input type="submit" value="Voltar"/>
{% csrf_token %}
</form>
{% if cadastrorealizadocomsucesso is True %}<b>cadastro realizado com sucesso!</b>{% endif %}
{% if error_message %}<p><strong>{{ error_message }}</p></strong>{% endif %}

So, every time the user forgets to mention a username or email or password in this screen, i use render to redirect the user to the same page but this time displaying a error_message. Let's say he did forget to fill the textfield with a username... he goes back to the same page and sees the error message. After that, let's say everything is right and he finally registers a new user, he sees "cadastro realizado com sucesso"(sucessfully registered new user) and stays on the same page. But when he goes back a page(pressing "back" on the browser), instead of going back to the page before the cadastro.html, he goes back to the same page cadastro.html but displaying the error message for the "you forgot to mention your username". I don't want him to go back to the same page with error message, i want to make him go back to my main menu. 

There is very little that you can do to prevent this type of behavior when the user presses the back button.

After the user successfully authenticates, they should be redirected to the proper landing page by using an HTTP 302 redirect. This is very common and baked in to all of the generic form handling views provided by Django. If the page is redirected, then the user will encounter a warning pop up from the browser asking if they want to resubmit the data. Generally this is enough to scare users away from using the back button, but even if they do continue through the warning, they'll simply reauthenticate and be redirected to the same landing page, again.

If that is not happening, them your form processing workflow is incorrect.

From your view, you aren't using Django forms at all. I would highly encourage you to do so, especially to better understand how form data should be processed and validated, and how to properly handle responses back to the client. The Django tutorial covers this topic. 

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciWeTfQL_XFL1ec7apdHJ8CBHEn0SO9-XBcjEtbA2ZPhVQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment