[Django]-How do you limit list objects template side, rather than view side

191👍

There is a slice filter that you can use in templates. This works exactly the same as slicing within the view.

{% for new in news|slice:":10" %}

13👍

You want to use the slice template filter

Here’s your example altered to use it:

<ul>
{% for new in news|slice:":3" %}
<li>
<p>{{ new.title }}</p>
<p>{{ new.body }}</p>
</li>
{% endfor %}
</ul>

Leave a comment