Django表单:无法从Django模板访问help_text

时间:2017-06-02 10:17:47

标签: django django-forms django-templates

使用Django 1.10。从docs开始,我似乎可以从模板访问help_text,但我无法做到。

class StudentForm(ModelForm):

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

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

在模板中,以下内容不打印:

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

1 个答案:

答案 0 :(得分:2)

您的模板没问题。问题是元选项应该是help_texts,而不是help_text

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

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