CSS error - Django Formfield

Asked

Viewed 93 times

0

This is giving the following error in my Django 1.9 form

See that on the label gets the :> in the end.

I DON’T WANT TO SEE THE LARGER SIGN (>) AT THE END OF THE LABEL Ex: Nome: >

inserir a descrição da imagem aqui

Follow my code forms.py

class FormContact(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 )

Even without the widget gets bug

HTML of contato.html

{% for field in form %}
    <div class="form-group">
        <label for={{ field.label_tag }}> {{ field_label_tag }} </label>
        {{ field }}
    </div>
{% endfor %}
  • What error are you getting?

  • @Denercarvalho in the label is showing the two points and the greater sign (:>). It should not appear.

1 answer

3


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

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

The {{ field.label_tag }} will already create for you the label of this field wrapped by the tag label. But as you are passing it inside a label, what happens is the following: when rendering the HTML, where you have the {{ field.label_tag }} it renders a full label tag, and disregard what comes before, which in this case is <label for=, but what comes after is only the >, that it renders like normal text anyway. Outside you have to remember that the content of an attribute has to stay inside quotes, as shown below.

You can see it here on 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_x suffiis a Colon:

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

In order for you to have the result you expect from the way you’re doing, you should use the {{ field.id_for_label }}, that would look like this:

{% for field in form %}
    <div class="form-group">
        <label for="{{ field.id_for_label }}"> {{ field.label }} </label>
        {{ field }}
    </div>
{% endfor %}
  • Excellent explanation. I will test and confirm if it worked

  • It worked just that. I had followed a tutorial from Trainings Web, I will warn the instructor tbm

Browser other questions tagged

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