Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django help_text

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?

like image 914
Shehzad009 Avatar asked Jan 25 '11 11:01

Shehzad009


2 Answers

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.

like image 133
S.Lott Avatar answered Sep 24 '22 11:09

S.Lott


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).

like image 32
Don Avatar answered Sep 24 '22 11:09

Don