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

21 views
Skip to first unread message

fábio andrews rocha marques

unread,
Sep 21, 2017, 1:46:36 PM9/21/17
to Django users
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?
login.png

fábio andrews rocha marques

unread,
Sep 21, 2017, 2:07:46 PM9/21/17
to Django users
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 }}>

James Schneider

unread,
Sep 21, 2017, 5:52:58 PM9/21/17
to django...@googlegroups.com
On Thu, Sep 21, 2017 at 11:07 AM, fábio andrews rocha marques <fabioandrews...@gmail.com> wrote:
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 }}>


You should be encapsulating the value of "value" in quotes:

<input id="usuario" type="text" name="usuario" value="{{ nomeusuariocadastro }}">

If you did that, you can also leave the / in place. I think it was interpreting the / as part of the value= rather than the closing of the tag due to the missing quotation marks.  

-James
Reply all
Reply to author
Forward
0 new messages