One of the ways to limit objects is to add a limitation to a function like this
def ten_objects():
obj = Model.objects.all()[0:10] # limit to 10
return {'objects': obj}
However how do you achieve this inside a template rather than inside a view?
I know you can filter through objects within a template and limit characters but how do you actually limit the amount of objects displayed in a loop. Via the template. for example the following code will loop through all objects....
<ul>
{% for new in news %}
<li>
<p>{{ news.title }}</p>
<p>{{ news.body }}</p>
</li>
{% endfor %}
</ul>
How do I break the loop, say after the firs 3 objects/items. And how do I do it inside the template. Thanks in advance.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…