Categories
django django-templates for-loop python python-3.x

add limit for list in for loop django template

I want to print only 10 elements from list in Django template

here is my code

<ul>
<h3>Positive Tweets :</h3>
{% for tweet in positiveTweet %}
<li>{{ tweet.0 }}</li>
{% endfor %}
</ul>

How can I print first 10 elements if positiveTweet list having length of 100 something.

You can use slice to make this:

<ul>
<h3>Positive Tweets :</h3>
{% for tweet in positiveTweet|slice:":10" %}
<li>{{ tweet.0 }}</li>
{% endfor %}
</ul>

See the Django Slice Docs.