[Answer]-Reference list items by index Django template

1👍

Use the forloop.first variable to determine the first form in the list:

{% for form in forms %}
<div class="{{ forloop.first|yesno:'image,question' }}">
   {{ form.as_p }}
</div>
{% endfor %}

P.S. You don’t need it for this case but to get the sublist in the template you can use the slice template filter.

Leave a comment