In my blog app I want to display a list of blog posts and the first image connected to this post. Now I do it this way:
{% for image in entry.image_set.all|slice:"1" %}
<img src="{{ image.get_absolute_url }}">
{% endfor %}
Is there a template shortcut I don't know about, or maybe I should just write my own Manager?
Not any shorter, but you could use first
:
{% with entry.image_set.all|first as image %}
<img src="{{ image.get_absolute_url }}">
{% endwith %}
Since Django 1.6 you can do
<img src="{{ entry.image_set.first.get_absolute_url }}">
You also can do:
entry.image_set.all.0
in your template.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With