Home > Blockchain >  DJANGO - How to associate help_text with each field in a custom template?
DJANGO - How to associate help_text with each field in a custom template?

Time:08-26

I'm using my own css and html to show a registration form. But I'm trying to use the default help_text that django provides for forms. I can show the help_text with the code below but I can't associate the errors with each field. That is, if I fail any of the requirements, it doesn't tell me which requirement failed, it just reloads the page without showing. I believe the error is because I'm not associating the error messages with each field.

.signup{    
    padding-top: 2.5rem;
    padding-bottom: 2.5rem;
    width: 320px;
    margin:auto 55px;
}

ul{
    padding-left: 15px;
}
.password1{
    margin-bottom: 15px;
}
.password2{
    margin-top: 15px;
}
.group            { 
  position: relative; 
  margin-bottom:45px; 
}
input               {
  font-size:18px;
  padding:10px 10px 10px 0px;
  display:block;
  width:320px;
  border:none;
  border-bottom:1px solid #999;
}
input:focus         { outline:none; }

/* LABEL ======================================= */
label                {
  color:#999; 
  font-size:18px;
  font-weight:normal;
  position:absolute;
  pointer-events:none;
  left:5px;
  top:10px;
  transition:0.2s ease all; 
  -moz-transition:0.2s ease all; 
  -webkit-transition:0.2s ease all;
}

/* active state */
input:focus ~ label, input:valid ~ label        {
  top:-20px;
  font-size:14px;
  color: black;
}

/* BOTTOM BARS ================================= */
.bar    { position:relative; display:block; width:320px; }
.bar:before, .bar:after     {
  content:'';
  height:2px; 
  width:0;
  bottom:1px; 
  position:absolute;
  background:black; 
  transition:0.2s ease all; 
  -moz-transition:0.2s ease all; 
  -webkit-transition:0.2s ease all;
}
.bar:before {
  left:50%;
}
.bar:after {
  right:50%; 
}

/* active state */
input:focus ~ .bar:before, input:focus ~ .bar:after {
  width:50%;
}

/* HIGHLIGHTER ================================== */
.highlight {
  position:absolute;
  height:60%; 
  width:100px; 
  top:25%; 
  left:0;
  pointer-events:none;
  opacity:0.5;
}

/* active state */
input:focus ~ .highlight {
  -webkit-animation:inputHighlighter 0.3s ease;
  -moz-animation:inputHighlighter 0.3s ease;
  animation:inputHighlighter 0.3s ease;
}

/* ANIMATIONS ================ */
@-webkit-keyframes inputHighlighter {
    from { background:#5264AE; }
  to    { width:0; background:transparent; }
}
@-moz-keyframes inputHighlighter {
    from { background:#5264AE; }
  to    { width:0; background:transparent; }
}
@keyframes inputHighlighter {
    from { background:#5264AE; }
  to    { width:0; background:transparent; }
}
{% extends "base.html" %}
{% load static %}
{% block title %}Sign Up{% endblock %}

{% block content %}
<link rel="stylesheet" href="{% static 'css/inputs.css' %}">
<link rel="stylesheet" href="{% static 'css/signup.css' %}">
<div >
  <a href="{% url 'login' %}"><- Voltar</a>
  <h1>Sign up</h1>
  <form action="" method="post">
    {% csrf_token %}
    <div >      
      <input type="email" name="username" required>
      <span ></span>
      <span ></span>
      <label>E-mail</label>
    </div>

    <div >      
      <input type="text" name="email" required>
      <span ></span>
      <span ></span>
      <label>Usuário</label>
    </div>

    <div >      
      <input type="text" name="first_name" required>
      <span ></span>
      <span ></span>
      <label>Primeiro nome</label>
    </div>

    <div >      
      <input type="text" name="last_name" required>
      <span ></span>
      <span ></span>
      <label>Último nome</label>
    </div>

    <div >      
      <input type="number" name="idade" required>
      <span ></span>
      <span ></span>
      <label>Idade</label>
    </div>

    <div >      
      <input type="text" name="telefone" required>
      <span ></span>
      <span ></span>
      <label>Telefone</label>
    </div>
    
    <div >      
      <input type="password" name="password1" required>
      <span ></span>
      <span ></span>
      <label>Senha</label>
    </div>
    
    {% for field in form %}
      {% if field.help_text %}
          {{ field.help_text }}
      {% endif %}
    {% endfor %}

    <div >      
      <input type="password" name="password2" required>
      <span ></span>
      <span ></span>
      <label>Confirme sua senha</label>
    </div>
    <button type="submit">Sign Up</button>
  </form> 


</div>


{% endblock %}

forms.py

class CustomUserCreationForm(UserCreationForm):

    class Meta:
        model = CustomUser
        fields = ('username','email', 'first_name', 'last_name', 'idade',  'telefone')

CodePudding user response:

Add in your template:

{% for field in form %}
    {% if field.errors %}
        {{ field.errors }}
    {% endif %}

    {% if field.help_text %}
        {{ field.help_text }}
    {% endif %}
{% endfor %}

Edit:
Why you don't do something like this?

{% if form.errors %}
   {% for error in form.errors %}
      {{ error }}
   {% endfor %}
{% endif %}
<form action="" method="post">
{% csrf_token %}
{% for field in form %}
    <div >      
      {{ field }}
      <span ></span>
      <span ></span>
      <label>{{ field.label }}</label>
      <span>{{ field.errors }}</span>
    </div>
{% endfor %}
</form>
  • Related