As of Django 1.2, if your form has an attribute named required_css_class
, it will be added to BoundField.css_classes
for required fields. You can then use CSS to style the required parts of the form as desired. A typical use case:
# views.py
class MyForm(django.forms.Form):
required_css_class = 'required'
…
…
/* CSS */
th.required { font-weight: bold; }
…
<!-- HTML -->
<tr>
<th class="{{form.name.css_classes}}">{{form.name.label_tag}}</th>
<td>{{form.name.errors}}{{form.name}}</td>
</tr>
If you use Form.as_table()
, Form.as_ul
, and Form.as_p
, they do this automatically, adding the class to <tr>
, <li>
, and <p>
, respectively.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…