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

Hmmm, yes, I have a slightly different situation then.

You might want to employ something with the query string.  Django, by default, uses "next" as a parameter on the Login screen.  When the user successfully logs in, they are redirected to the URL specified in the "next" GET parameter.  It doesn't address the issue of pressing the Back button, but the user may be discouraged from pressing the back button if they actually end up on the page that they were expecting to be at in the first place.

 

From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of fábio andrews rocha marques
Sent: Tuesday, October 31, 2017 1:02 PM
To: Django users
Subject: 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

 

How did you use the never_cache? Because if i just put @never_cache before the def of my view: def cadastrarprofessor(request):, it's like it doesn't do anything (i am on the login page, I go to the registration page, inform a name of a user that already exists, click "register", the system refreshes the page telling me that the user already exists and when i press the "back button" from the browser, it goes back to the registration page with empty fields, which is not what iwant. I want to go back to the login page, the page before the registration). Do I have to use @never_cache and remove all the return render from my function? What should i return instead?
On Tuesday, October 31, 2017 at 2:46:01 PM UTC-3, Matthew Pava wrote:

I had a similar issue and even tried using sessions, but I eventually found that never_cache worked beautifully for me.  I didn't have to worry about sessions any more.

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of fábio andrews rocha marques
Sent: Tuesday, October 31, 2017 12:19 PM
To: Django users
Subject: 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

 

I found this topic within stackoverflow: https://stackoverflow.com/questions/10906548/django-redirect-behavior-with-back-button

 

 that seems to me that the user was having the same problem as i am. The only answer i found could work was from magicTuscan, but he just said "I would save the last site path as a session, then retrieve via the view when the page is called again via the back button". I've tried to contact him but didn't get an answer. Is it possible to override the "browser back button" behavior on django using sessions?

--
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...@googlegroups.com.
To post to this group, send email to djang...@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/3ad05d02-cecd-4560-9654-82d2ed1106ca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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/fa86e291-ad2e-4b5a-b78a-1e98327189ce%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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 31, 2017 11:02 AM, "fábio andrews rocha marques" <fabioandrewsrochamarques@gmail.com> wrote:
How did you use the never_cache? Because if i just put @never_cache before the def of my view: def cadastrarprofessor(request):, it's like it doesn't do anything (i am on the login page, I go to the registration page, inform a name of a user that already exists, click "register", the system refreshes the page telling me that the user already exists and when i press the "back button" from the browser, it goes back to the registration page with empty fields, which is not what iwant. I want to go back to the login page, the page before the registration). Do I have to use @never_cache and remove all the return render from my function? What should i return instead?

I would recommend you remove any modifications related to caching. Caching is not your issue, and in most cases should only be tuned in specific circumstances, and after your intended functionality is implemented and working. Refer to my other thread for a different direction.

-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%2BciWmyNJNu7Uq%3DE0wnnwygoOJ3r5Uh8D1KTNt6MOYHMe8qg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

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.

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

How did you use the never_cache? Because if i just put @never_cache before the def of my view: def cadastrarprofessor(request):, it's like it doesn't do anything (i am on the login page, I go to the registration page, inform a name of a user that already exists, click "register", the system refreshes the page telling me that the user already exists and when i press the "back button" from the browser, it goes back to the registration page with empty fields, which is not what iwant. I want to go back to the login page, the page before the registration). Do I have to use @never_cache and remove all the return render from my function? What should i return instead?
On Tuesday, October 31, 2017 at 2:46:01 PM UTC-3, Matthew Pava wrote:

I had a similar issue and even tried using sessions, but I eventually found that never_cache worked beautifully for me.  I didn't have to worry about sessions any more.

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of fábio andrews rocha marques
Sent: Tuesday, October 31, 2017 12:19 PM
To: Django users
Subject: 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

 

I found this topic within stackoverflow: https://stackoverflow.com/questions/10906548/django-redirect-behavior-with-back-button

 

 that seems to me that the user was having the same problem as i am. The only answer i found could work was from magicTuscan, but he just said "I would save the last site path as a session, then retrieve via the view when the page is called again via the back button". I've tried to contact him but didn't get an answer. Is it possible to override the "browser back button" behavior on django using sessions?

--
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...@googlegroups.com.
To post to this group, send email to djang...@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/3ad05d02-cecd-4560-9654-82d2ed1106ca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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/fa86e291-ad2e-4b5a-b78a-1e98327189ce%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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

I had a similar issue and even tried using sessions, but I eventually found that never_cache worked beautifully for me.  I didn't have to worry about sessions any more.

 

From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of fábio andrews rocha marques
Sent: Tuesday, October 31, 2017 12:19 PM
To: Django users
Subject: 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

 

I found this topic within stackoverflow: https://stackoverflow.com/questions/10906548/django-redirect-behavior-with-back-button

 

 that seems to me that the user was having the same problem as i am. The only answer i found could work was from magicTuscan, but he just said "I would save the last site path as a session, then retrieve via the view when the page is called again via the back button". I've tried to contact him but didn't get an answer. Is it possible to override the "browser back button" behavior on django using sessions?

--
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/3ad05d02-cecd-4560-9654-82d2ed1106ca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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

I found this topic within stackoverflow: https://stackoverflow.com/questions/10906548/django-redirect-behavior-with-back-button

 that seems to me that the user was having the same problem as i am. The only answer i found could work was from magicTuscan, but he just said "I would save the last site path as a session, then retrieve via the view when the page is called again via the back button". I've tried to contact him but didn't get an answer. Is it possible to override the "browser back button" behavior on django using sessions?

--
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/3ad05d02-cecd-4560-9654-82d2ed1106ca%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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

Yes, my code has an else condition that always returns a render. The error_message is populated according to the error made by the user, like "forgot your username","forgot password", "username already exists".

Since i always need to return an httpresponse or render in my django view, will the page always refresh?

Here's the full code of my view currently:

def cadastrarprofessor(request):
if 'terminarcadastroprofessor' in request.POST:
# if this is a POST request we need to process the form data
if request.method == 'POST':
nomeusuario = request.POST['usuario']
nomesenha = request.POST['senha']
nomeemail = request.POST['email']
print("nomeusuario:")
print(nomeusuario)
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})
elif User.objects.filter(username=nomeusuario).exists():
return render(request,'cadastro.html',{'cadastrorealizadocomsucesso': False, 'error_message': "nome de usuário já existe", 'nomesenhacadastro':nomesenha, 'nomeusuariocadastro':nomeusuario, 'nomeemailcadastro':nomeemail})
else:
usuarionovo = User.objects.create_user(nomeusuario, nomeemail, nomesenha)
novoprofessor = Professor(user=usuarionovo,nome=nomeusuario)
novoprofessor.save()
return render(request,'cadastro.html',{'cadastrorealizadocomsucesso': True})
else:
return render(request,'index.html')
else:
#vou para tela do inicio do cadastro. Era um link entao nao tem POST
return render(request, 'cadastro.html',{'cadastrorealizadocomsucesso':False})





On Tuesday, October 31, 2017 at 1:47:31 PM UTC-3, Matthew Pava wrote:

> to do this, i just have to put the @never_cache on my View's method, right?

 

Yes.

 

Upon further investigation of your code, I suggest looking into Django forms and utilizing them.  Also, you shouldn't need to use two forms on your log-in view.  The submit button should go inside the log-in form.

 

To get back to fixing your current code, do you have an else condition that always returns a render?  And, yes, you need to return an HttpResponse object in all of your view functions, which is returned by the render function.  It looks like you are always populating 'error_message,' which is why there is always an error_message rendered.

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of fábio andrews rocha marques
Sent: Tuesday, October 31, 2017 11:06 AM
To: Django users
Subject: 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

 

to do this, i just have to put the @never_cache on my View's method, right? (after importing 'from django.views.decorators.cache import never_cache'). For some reason, when i did this, the page started and i already got the message "usuário já existe"(username already exists) and still the problem i stated occurred. Is it something i am doing wrong? Like, i should remover the return renders from my function after putting @never_cache?

On Tuesday, October 31, 2017 at 11:25:28 AM UTC-3, Matthew Pava wrote:

Try using the @never_cache decorator on your login view.

https://docs.djangoproject.com/en/1.11/topics/http/decorators/#django.views.decorators.cache.never_cache

 

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of fábio andrews rocha marques
Sent: Tuesday, October 31, 2017 9:15 AM
To: Django users
Subject: 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

 

That's a great suggestion. Sure, it will help on situations where i just want the fields to be completed. But there are some fields that require a little more feedback than that. Like, if a username already exists... I check if it already exists using a View and checking on the database. So, after the View checks if the user already exists, i have to render the page again to show the error. 

On Tuesday, October 31, 2017 at 8:37:05 AM UTC-3, Eric Pascual wrote:

Hello
Sorry or I misunderstood your problem but what about using the required field attribute ? It will make such controls and messages be handled directly on the client side.
Best regards.

Eric PASCUAL

Centre Scientifique et Technique du Bâtiment
290 route des Lucioles
06560 SOPHIA ANTIPOLIS
http://www.cstb.fr


________________________________________
From: django...@googlegroups.com [django...@googlegroups.com] on behalf of fábio andrews rocha marques [fabioandrews...@gmail.com]
Sent: Tuesday, October 31, 2017 00:46
To: Django users
Subject: 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

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.
Is there a better approach to display error messages on the same page instead of using return render like this:
return render(request,'cadastro.html',{'cadastrorealizadocomsucesso': False, 'error_message': "informe um usuário", 'nomeemailcadastro':nomeemail,'nomesenhacadastro':nomesenha})
?


--
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...@googlegroups.com<mailto:django-users+unsubscribe@googlegroups.com>.
To post to this group, send email to django...@googlegroups.com<mailto:django...@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/3f70cd29-9edc-4272-a27e-534ee7f1e1a2%40googlegroups.com<https://groups.google.com/d/msgid/django-users/3f70cd29-9edc-4272-a27e-534ee7f1e1a2%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
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...@googlegroups.com.
To post to this group, send email to djang...@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/f73d478e-44ee-40dd-ba61-851f0e0ec5af%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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...@googlegroups.com.
To post to this group, send email to djang...@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/ad9b6c53-88be-4825-98de-e9d1088fb4fe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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/87265145-9162-4276-bffb-40fd7c4b8710%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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

> to do this, i just have to put the @never_cache on my View's method, right?

 

Yes.

 

Upon further investigation of your code, I suggest looking into Django forms and utilizing them.  Also, you shouldn't need to use two forms on your log-in view.  The submit button should go inside the log-in form.

 

To get back to fixing your current code, do you have an else condition that always returns a render?  And, yes, you need to return an HttpResponse object in all of your view functions, which is returned by the render function.  It looks like you are always populating 'error_message,' which is why there is always an error_message rendered.

 

From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of fábio andrews rocha marques
Sent: Tuesday, October 31, 2017 11:06 AM
To: Django users
Subject: 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

 

to do this, i just have to put the @never_cache on my View's method, right? (after importing 'from django.views.decorators.cache import never_cache'). For some reason, when i did this, the page started and i already got the message "usuário já existe"(username already exists) and still the problem i stated occurred. Is it something i am doing wrong? Like, i should remover the return renders from my function after putting @never_cache?

On Tuesday, October 31, 2017 at 11:25:28 AM UTC-3, Matthew Pava wrote:

Try using the @never_cache decorator on your login view.

https://docs.djangoproject.com/en/1.11/topics/http/decorators/#django.views.decorators.cache.never_cache

 

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of fábio andrews rocha marques
Sent: Tuesday, October 31, 2017 9:15 AM
To: Django users
Subject: 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

 

That's a great suggestion. Sure, it will help on situations where i just want the fields to be completed. But there are some fields that require a little more feedback than that. Like, if a username already exists... I check if it already exists using a View and checking on the database. So, after the View checks if the user already exists, i have to render the page again to show the error. 

On Tuesday, October 31, 2017 at 8:37:05 AM UTC-3, Eric Pascual wrote:

Hello
Sorry or I misunderstood your problem but what about using the required field attribute ? It will make such controls and messages be handled directly on the client side.
Best regards.

Eric PASCUAL

Centre Scientifique et Technique du Bâtiment
290 route des Lucioles
06560 SOPHIA ANTIPOLIS
http://www.cstb.fr


________________________________________
From: django...@googlegroups.com [django...@googlegroups.com] on behalf of fábio andrews rocha marques [fabioandrews...@gmail.com]
Sent: Tuesday, October 31, 2017 00:46
To: Django users
Subject: 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

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.
Is there a better approach to display error messages on the same page instead of using return render like this:
return render(request,'cadastro.html',{'cadastrorealizadocomsucesso': False, 'error_message': "informe um usuário", 'nomeemailcadastro':nomeemail,'nomesenhacadastro':nomesenha})
?


--
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...@googlegroups.com<mailto:django-users+unsubscribe@googlegroups.com>.
To post to this group, send email to django...@googlegroups.com<mailto:django...@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/3f70cd29-9edc-4272-a27e-534ee7f1e1a2%40googlegroups.com<https://groups.google.com/d/msgid/django-users/3f70cd29-9edc-4272-a27e-534ee7f1e1a2%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
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...@googlegroups.com.
To post to this group, send email to djang...@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/f73d478e-44ee-40dd-ba61-851f0e0ec5af%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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/ad9b6c53-88be-4825-98de-e9d1088fb4fe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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

to do this, i just have to put the @never_cache on my View's method, right? (after importing 'from django.views.decorators.cache import never_cache'). For some reason, when i did this, the page started and i already got the message "usuário já existe"(username already exists) and still the problem i stated occurred. Is it something i am doing wrong? Like, i should remover the return renders from my function after putting @never_cache?

On Tuesday, October 31, 2017 at 11:25:28 AM UTC-3, Matthew Pava wrote:

Try using the @never_cache decorator on your login view.

https://docs.djangoproject.com/en/1.11/topics/http/decorators/#django.views.decorators.cache.never_cache

 

 

From: django...@googlegroups.com [mailto:django...@googlegroups.com] On Behalf Of fábio andrews rocha marques
Sent: Tuesday, October 31, 2017 9:15 AM
To: Django users
Subject: 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

 

That's a great suggestion. Sure, it will help on situations where i just want the fields to be completed. But there are some fields that require a little more feedback than that. Like, if a username already exists... I check if it already exists using a View and checking on the database. So, after the View checks if the user already exists, i have to render the page again to show the error. 

On Tuesday, October 31, 2017 at 8:37:05 AM UTC-3, Eric Pascual wrote:

Hello
Sorry or I misunderstood your problem but what about using the required field attribute ? It will make such controls and messages be handled directly on the client side.
Best regards.

Eric PASCUAL

Centre Scientifique et Technique du Bâtiment
290 route des Lucioles
06560 SOPHIA ANTIPOLIS
http://www.cstb.fr


________________________________________
From: django...@googlegroups.com [django...@googlegroups.com] on behalf of fábio andrews rocha marques [fabioandrews...@gmail.com]
Sent: Tuesday, October 31, 2017 00:46
To: Django users
Subject: 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

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.
Is there a better approach to display error messages on the same page instead of using return render like this:
return render(request,'cadastro.html',{'cadastrorealizadocomsucesso': False, 'error_message': "informe um usuário", 'nomeemailcadastro':nomeemail,'nomesenhacadastro':nomesenha})
?


--
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...@googlegroups.com<mailto:django-users+unsubscribe@googlegroups.com>.
To post to this group, send email to django...@googlegroups.com<mailto:django...@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/3f70cd29-9edc-4272-a27e-534ee7f1e1a2%40googlegroups.com<https://groups.google.com/d/msgid/django-users/3f70cd29-9edc-4272-a27e-534ee7f1e1a2%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
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...@googlegroups.com.
To post to this group, send email to djang...@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/f73d478e-44ee-40dd-ba61-851f0e0ec5af%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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/ad9b6c53-88be-4825-98de-e9d1088fb4fe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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

Try using the @never_cache decorator on your login view.

https://docs.djangoproject.com/en/1.11/topics/http/decorators/#django.views.decorators.cache.never_cache

 

 

From: django-users@googlegroups.com [mailto:django-users@googlegroups.com] On Behalf Of fábio andrews rocha marques
Sent: Tuesday, October 31, 2017 9:15 AM
To: Django users
Subject: 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

 

That's a great suggestion. Sure, it will help on situations where i just want the fields to be completed. But there are some fields that require a little more feedback than that. Like, if a username already exists... I check if it already exists using a View and checking on the database. So, after the View checks if the user already exists, i have to render the page again to show the error. 

On Tuesday, October 31, 2017 at 8:37:05 AM UTC-3, Eric Pascual wrote:

Hello
Sorry or I misunderstood your problem but what about using the required field attribute ? It will make such controls and messages be handled directly on the client side.
Best regards.

Eric PASCUAL

Centre Scientifique et Technique du Bâtiment
290 route des Lucioles
06560 SOPHIA ANTIPOLIS
http://www.cstb.fr


________________________________________
From: django...@googlegroups.com [django...@googlegroups.com] on behalf of fábio andrews rocha marques [fabioandrews...@gmail.com]
Sent: Tuesday, October 31, 2017 00:46
To: Django users
Subject: 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

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.
Is there a better approach to display error messages on the same page instead of using return render like this:
return render(request,'cadastro.html',{'cadastrorealizadocomsucesso': False, 'error_message': "informe um usuário", 'nomeemailcadastro':nomeemail,'nomesenhacadastro':nomesenha})
?


--
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...@googlegroups.com<mailto:django-users+unsubscribe@googlegroups.com>.
To post to this group, send email to django...@googlegroups.com<mailto:django...@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/3f70cd29-9edc-4272-a27e-534ee7f1e1a2%40googlegroups.com<https://groups.google.com/d/msgid/django-users/3f70cd29-9edc-4272-a27e-534ee7f1e1a2%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
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/f73d478e-44ee-40dd-ba61-851f0e0ec5af%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

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

That's a great suggestion. Sure, it will help on situations where i just want the fields to be completed. But there are some fields that require a little more feedback than that. Like, if a username already exists... I check if it already exists using a View and checking on the database. So, after the View checks if the user already exists, i have to render the page again to show the error. 

On Tuesday, October 31, 2017 at 8:37:05 AM UTC-3, Eric Pascual wrote:
Hello
Sorry or I misunderstood your problem but what about using the required field attribute ? It will make such controls and messages be handled directly on the client side.
Best regards.

Eric PASCUAL

Centre Scientifique et Technique du Bâtiment
290 route des Lucioles
06560 SOPHIA ANTIPOLIS
http://www.cstb.fr


________________________________________
From: django...@googlegroups.com [django...@googlegroups.com] on behalf of fábio andrews rocha marques [fabioandrews...@gmail.com]
Sent: Tuesday, October 31, 2017 00:46
To: Django users
Subject: 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

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.
Is there a better approach to display error messages on the same page instead of using return render like this:
return render(request,'cadastro.html',{'cadastrorealizadocomsucesso': False, 'error_message': "informe um usuário", 'nomeemailcadastro':nomeemail,'nomesenhacadastro':nomesenha})
?


--
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...@googlegroups.com<mailto:django-users+unsubscribe@googlegroups.com>.
To post to this group, send email to django...@googlegroups.com<mailto:django...@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/3f70cd29-9edc-4272-a27e-534ee7f1e1a2%40googlegroups.com<https://groups.google.com/d/msgid/django-users/3f70cd29-9edc-4272-a27e-534ee7f1e1a2%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

--
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/f73d478e-44ee-40dd-ba61-851f0e0ec5af%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.