Ayush Srivastava Ayush Srivastava - 3 months ago 14
Python Question

access first message in Django template

I am using the following code to access the first message in Django template,

{% if messages %}
{% for message in messages %}
{% if forloop.first %}
{{ message }}
{% endif %}
{% endfor %}
{% endif %}


How I can achieve the same without using the for loop in a single statement.

Answer

Following should work:

{% if messages %}
    {{ messages.0 }}
{% endif %}
Comments