Django help_text

I would like to add help text in this form.

class ItemForm(forms.ModelForm): alternative_id = forms.CharField(max_length = 60, required = False, help_text = 'Valid wildcard search is in the format *XX, *XX*, XX*') 

However, it does not appear on the page. Is it because I might need a template tag somewhere?

EDIT . I wrote this in my template.

 <div id="location_header">Search for Items</div> <div id="form_container"> <form action="." method="post"> <fieldset class="model"> {{ form.as_p }} {{ alternative_id.help_text }} </fieldset> <div id="form_footer"> <input type="submit" value="Search" > </div> </form> </div> 

Help text is still not displayed. Is there a way to write help text allowing django to generate a form?

+4
source share
3 answers

Putting {{ form.as_p }} (or just {{ form }} ) in your template should display help_text without additional code, provided that you have a form in your context (but I suppose you will do this if you get field on your page).

+2
source

Step 1. Read: https://docs.djangoproject.com/en/1.8/topics/forms/#looping-over-the-form-s-fields

Step 2. Configure. Here are the rules.

In this loop, {{field}} is an instance of BoundField. BoundField also has the following attributes that may be useful in your templates:

{{field.label}} The label field, for example. E-mail address.

{{field.label_tag}} A label field wrapped in the corresponding HTML tag, for example. E-mail address

{{field.html_name}} The name of the field to be used in the input name of the element. This requires a prefix form if it has been installed.

{{field.help_text}} Any help text that has been associated with the field.

{{field.errors}} Prints a containing any validation errors matching this field. You can customize the error view with {% for error in field.errors%} loop. In this case, each object in the loop is a simple line containing an error message.

+17
source

I see that something is missing in your recycling if the ItemForm instance is passed in the template as {{form}} try {{ form.alternative_id.help_text }} .. as the docs in @ S.Lott's answer say.

0
source

Source: https://habr.com/ru/post/1336949/


All Articles