tags:

views:

109

answers:

3

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?

+1  A: 

{{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

Xealot
Yeah... I read that documentation, and it's missing the two properties I need. `label_tag` is *not* what I want. I can't include the colon inside the `<label>` if I use it.
Mark
You're right, sorry I ready it too quickly. {{field.field}} is definitely what you should be using after reviewing my own code.
Xealot
+1  A: 

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 :\

Mark
+1  A: 

You can get the input field's id attribute like this: {{ field.auto_id }}

Ondrej
thank you. i had found the answer already but forgot to check it.
Mark