Thursday, September 21, 2017

Re: coming back to register user page after not filling a text field is causing the already filled text fields come with an annexed "/" at the end of the values

Solved it: If i remove the "/" from all my <input> tags, it's solved.
Before:
<input id="usuario" type="text" name="usuario" value={{ nomeusuariocadastro }}/>
After:
<input id="usuario" type="text" name="usuario" value={{ nomeusuariocadastro }}>

On Thursday, September 21, 2017 at 2:46:36 PM UTC-3, fábio andrews rocha marques wrote:
My register User View is like this:

def cadastrarprofessor(request):
# 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']
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(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()
url = reverse('fimcadastro')
return HttpResponseRedirect(url)

So, if a user doesn't fill all the textfields from the register page, he'll come back to a page 'cadastro' but if he already filled a username, email or password text fields, i wanted to make him go back to the same page without making all textfields go blank again.
My template 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 %}
<label for="senha">Senha: </label>
{% if nomesenhacadastro %}
<input id="senha" type="text" name="senha" value={{ nomesenhacadastro }}/>
{% else %}
<input id="senha" type="text" name="senha" value=""/>
{% endif %}
<label for="email">Email: </label>
{% if nomeemailcadastro %}
<input id="email" type="text" name="email" value= {{ nomeemailcadastro }}/>
{% else %}
<input id="email" type="text" name="email" value=""/>
{% endif %}
<input type="submit" value="Cadastrar"/>
</form>
{% if cadastrorealizadocomsucesso is True %}<b>cadastro realizado com sucesso!</b>{% endif %}
{% if error_message %}<p><strong>{{ error_message }}</p></strong>{% endif %}

 For some reason, when the user returns to the same page but already did fill a text field, it'll always return the value including some slashes. Like on the annex, where he already informed a password(senha) and an username(usuário), but not an email. The system informs that he must inform an email, but check those text fields! They have slashes at the end of all the values! How can i return the textfields without the slashes in the end?

--
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/e5401770-84c5-415e-8c7e-54264dfaaa69%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment