Form label is not shown on the #Django page

Asked

Viewed 110 times

0

I created a customer registration form and in the meta class I declared the Fields and their respective Abels. However the label 'CPF' referring to the field 'n_cpf' does not show on the page, only appears N Cpf. How do I label 'CPF' in this field?

Forms.py:

from django import  forms
from .models import Cliente

from localflavor.br.forms import BRCPFField

class ClienteForm(forms.ModelForm):
    n_cpf = BRCPFField()
    class Meta:
        model = Cliente
        fields = ['name', 'last_name', 'n_cpf', 'birth_date', 'height', 'weight', 'active']
        labels = {'name':       'Nome', 
                  'last_name':  'Sobrenome',
                  'n_cpf':      'CPF',
                  'birth_date': 'Data de nascimento',
                  'height':     'Altura',
                  'weight':     'Peso',
                  'active':     'Ativo',
                  }

html register.:

{% extends 'app_web/base.html' %}

{% block content %}
    <p>Adicione um novo cliente:</p>
    <form action="{% url 'app_web:novo_cliente' %}" method="POST">
        {% csrf_token %}
        {{ form.as_p }}
        <button name="submit">Adicionar</button>
    </form>
{% endblock content %}

inserir a descrição da imagem aqui

  • Could you show the html page where the form is being displayed? Note that you can display the label by typing in html, for example {{ form.cpf.label_tag }}

  • OK friend put the html. I’m starting in Django, in the html form I use only this double bracket '{{ }}' to interpolate the variable.

1 answer

1


Try the following:

n_cpf = BRCPFField(label='CNPJ')

<form action="{% url 'app_web:novo_cliente' %}" method="POST">
    {% csrf_token %}
    {% for field in form %}
        <p>
            {{ field.label_tag }}
            {{ field }}
            {{ field.help_text }}
            {{ field.errors }}
        </p>
    {% endfor %}
    <button name="submit">Adicionar</button>
</form>

This way you will still render the form without having to write the whole form, but it will make it easier for you to move the form, like doing if’s inside the form for example.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.