Anupam
Anupam

Reputation: 15620

Django form: can't access help_text from Django template

Using Django 1.10. From the docs, it seems that I should be able to access help_text from the template but I can't.

class StudentForm(ModelForm):

    class Meta:
        model = Student
        fields = ['name', 'age']

        help_text = {
            'age': "enter your age in years and months",
        }

In the template, the following prints nothing:

{%for field in form %}
    {% if field.help_text %}
        <p class="help">{{ field.help_text|safe }}</p>
    {% endif %}
{% endfor %}

Upvotes: 1

Views: 284

Answers (1)

Alasdair
Alasdair

Reputation: 308829

Your template is OK. The problem is that the meta option should be help_texts, not help_text.

class Meta:
    model = Student
    fields = ['name', 'age']

    help_texts = {
        'age': "enter your age in years and months",
    }

Upvotes: 3

Related Questions