Still learning django and python…
I have a form with many fields. I want to lay them out in rows of two fields each, and am using a table to do so. I can’t simply iterate over all the fields as I want finer control on certain things.
Essentially, I am putting out a row of two fields, and then if there are errors in any of those fields, generate a second row containing the errors.
In the template, I am repeating the following pattern over and over again:
<tr>
<td class="labels">Order Number:</td><td class="textentry">{{ order.ordernum }}</td>
<td class="labels">Order Status:</td><td class="textentry">{{ order.status }} </td>
</tr>
{% if order.ordernum.errors %}
{% for error in order.ordernum.errors %}
<tr class="errors"><td colspan=2> {{ error|escape }}</td>
{% endfor %}
{% if order.status.errors %}
{% for error in order.status.errors %}
<td colspan=2> {{ error|escape }}</td>
{% endfor %}
{% endif %}
</tr>
{% endif %}
where all that changes from repeated pattern to repeated pattern are the specific field names.
The repetition makes me wonder: is there a better way to do this in the template? Is there some way to call a function from within the template where I could just pass in the field names?
Thanks for your help!
W.
For anyone interested, I don’t know if there’s a better solution, but template macros a la djangosnippets.org/snippets/363 is how I solved this problem.