I'm trying to customize how my form is displayed by using a form_snippet as suggested in the docs. Here's what I've come up with so far:
{% for field in form %}
<tr>
开发者_高级运维 <th><label for="{{ field.html_name }}">{{ field.label }}:</label></th>
<td>
{{ field }}
{% if field.help_text %}<br/><small class="help_text">{{ field.help_text }}</small>{% endif %}
{{ field.errors }}
</td>
</tr>
{% endfor %}
Of course, field.html_name
is not what I'm looking for. I need the id
of the input field. How can I get that?
Also, is there a way I can determine if the field is required, so that I can display an asterisk beside the label?
Found both answers here. My new script looks like this:
{% for field in form %}
<tr>
<th>{% if field.field.required %}*{% endif %}<label for="{{ field.auto_id }}">{{ field.label }}:</label></th>
<td>
{{ field }}
{% if field.help_text %}<br/><small class="help_text">{{ field.help_text }}</small>{% endif %}
{{ field.errors }}
</td>
</tr>
{% endfor %}
Stupid incomplete docs :\
{{field.label_tag}} should have what you're looking for to populate the 'for' attribute on the label.
You might want to try {{field.required}} and see if it works, I seem to remember something like that in my own forms.
http://docs.djangoproject.com/en/dev/topics/forms/#looping-over-the-form-s-fields
You can get the input field's id attribute like this: {{ field.auto_id }}
精彩评论