Django

Code

Changeset 6740

Show
Ignore:
Timestamp:
11/29/07 11:30:01 (1 year ago)
Author:
mtredinnick
Message:

Fixed #5854 -- Added an example to the newforms documentation showing how to
highlight required fields (which should also provide enough clues for
accessing other attributes on newforms.Field subclasses. Thanks,
christobzr@gmail.com.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r6733 r6740  
    189189    Joseph Kocherhans 
    190190    konrad@gwu.edu 
     191    knox <christobzr@gmail.com> 
    191192    kurtiss@meetro.com 
    192193    lakin.wecker@gmail.com 
  • django/trunk/docs/newforms.txt

    r6733 r6740  
    753753    </ul> 
    754754    </form> 
     755 
     756Highlighting required fields in templates 
     757~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
     758 
     759You may wish to show a visitor which fields are required. Here is the above 
     760example modified to insert an asterix after the label of each required field:: 
     761 
     762    <form method="post" action=""> 
     763    <dl> 
     764    {% for field in form %} 
     765        <dt>{{ field.label_tag }}{{ field.label }}{% if field.field.required %}*{% endif %}</dt> 
     766        <dd>{{ field }}</dd> 
     767        {% if field.help_text %}<dd>{{ field.help_text }}</dd>{% endif %} 
     768        {% if field.errors %}<dd class="myerrors">{{ field.errors }}</dd>{% endif %} 
     769    {% endfor %} 
     770    </dl> 
     771    <input type="submit" /> 
     772    </form> 
     773 
     774The ``{% if field.field.required %}*{% endif %}`` fragment is the relevant 
     775addition here. It adds the asterix only if the field is required. Note that we 
     776check ``field.field.required`` and not ``field.required``. In the template, 
     777``field`` is a ``newforms.forms.BoundField`` instance, which holds the actual 
     778``Field`` instance in its ``field`` attribute. 
    755779 
    756780Binding uploaded files to a form