Question

Is there a way to access a specific form from an inline formset, in the template, without hardcoding the index? I know that the usual way to iterate through a formset is to do something like:

{% for form in formset %}
    {{ form }}
{% endfor %}

But due to some details on the template (i have multiple formsets, that should be displayed side by side on a table, inside another for), it would be better if i could access each form by its index. I can do this by hardcoding the index, like {{ formset.0 }}, but since i'm iterating in the template, the ideal would be to get the form by the forloop.counter, so that i could do something like

{% for field in fields %}
<tr>
    <td>{{ field }}</td>
    <td>{{ formset1.[forloop.counter0] }}</td>
    <td>{{ formset2.[forloop.counter0] }}</td>
</tr>
{% endfor %}

Is there a way to achive this?

Was it helpful?

Solution

Custom indexing isn't possible inside template.

You can achieve the same result by creating your own filter. See the following snippet:

http://djangosnippets.org/snippets/2740/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top