How to break "for loop" in Django template
I have this code
{% for account in object_list %}
<tr>
{% for field, value in book.get_fields %}
<th>{{ field.verbose_name }}</th>
{% endfor %}
</tr>
{{ break }}
{% endfor %}
I want to break the for loop after first iteration. break is not working
Solution 1:
I think you should use slice to achieve your goal
{% for account in object_list|slice:":1" %}
Solution 2:
There is no break
in Django template system. Django template system is not programmed with python but with its own language.
Depending on what you need to do, you might find this question useful. Otherwise, just put the one and only account you are trying to print on HTML on a special field on your RequestContext
.