Django help_text

19,699

Solution 1

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

Solution 2

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

Step 2. Customize. Here are the rules.

Within this loop, {{ field }} is an instance of BoundField. BoundField also has the following attributes, which can be useful in your templates:

{{ field.label }} The label of the field, e.g. E-mail address.

{{ field.label_tag }} The field's label wrapped in the appropriate HTML tag, e.g. E-mail address

{{ field.html_name }} The name of the field that will be used in the input element's name field. This takes the form prefix into account, if it has been set.

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

{{ field.errors }} Outputs a containing any validation errors corresponding to this field. You can customize the presentation of the errors with a {% for error in field.errors %} loop. In this case, each object in the loop is a simple string containing the error message.

Solution 3

I see something is missing in your snipped, if the ItemForm instance is passed in template as {{ form }} try {{ form.alternative_id.help_text }}.. as the docs says in the @S.Lott answer.

Share:
19,699
Shehzad009
Author by

Shehzad009

Updated on June 04, 2022

Comments

  • Shehzad009
    Shehzad009 almost 2 years

    I would like to put some 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 may need a template tag somewhere?

    EDIT: I have this written 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 still does not appear. Is there a way to write a help text, while allowing django to generate a form?

  • user1066101
    user1066101 over 13 years
    @Shehzad009: Your code looks nothing like the example in the Django documentation. Please read the supplied link.
  • Shehzad009
    Shehzad009 over 13 years
    Of course, because by the looks of things, does not look like I can use form.as_p to generate the form.