Error in CSS - Django FormField

0

I'm giving the following error in my Django 1.9 form

Notice that the label has the :> at the end.

I DO NOT WANT THE GREATEST SIGN (>) AT THE END OF LABEL Ex: Nome: >

Followmycodeforms.py

classFormContact(forms.Form):name=forms.CharField(label='Nome',widget=forms.TextInput(attrs={'class':'form-control'}))email=forms.EmailField(label='Email',widget=forms.TextInput(attrs={'class':'form-control'}))phone=forms.CharField(label='Telefone',max_length=13,widget=forms.TextInput(attrs={'class':'form-control'}))message=forms.CharField(label='Mensagem',widget=forms.Textarea(attrs={'class':'form-control'}),max_length=500)

Evenwithoutthewidgetyougeterror

HTMLofcontato.html

{%forfieldinform%}<divclass="form-group">
        <label for={{ field.label_tag }}> {{ field_label_tag }} </label>
        {{ field }}
    </div>
{% endfor %}
    
asked by anonymous 17.02.2016 / 02:04

1 answer

3

The problem is that you are using unnecessary code. Look at this part:

{% for field in form %}
    <div class="form-group">
        <label for={{ field.label_tag }}> {{ field_label_tag }} </label>
        {{ field }}
    </div>
{% endfor %}

{{ field.label_tag }} will already create for you the label of this field wrapped by the label tag. But as you are passing it inside a label, what happens is this: at the time of rendering HTML, where it has {{ field.label_tag }} it renders a complete label tag, and disregard what comes before, which in the case is <label for= , but what comes next is only > , which it renders as normal text itself. Outside you have to remember that the content of an attribute has to be within quotes, as I show below.

You can see this here at documentation >

  

{{field.label_tag}}

     

The field's label wrapped in the appropriate HTML tag. This   includes the form's label_suffix. For example, the default   label_suffix is a colon:

     

<label for="id_email">Email address:</label>

For you to get the result you expect the way you are doing, you should use {{ field.id_for_label }} , which would look like this:

{% for field in form %}
    <div class="form-group">
        <label for="{{ field.id_for_label }}"> {{ field.label }} </label>
        {{ field }}
    </div>
{% endfor %}
    
17.02.2016 / 14:09