R
R
Ruslan Bergutov2015-05-15 07:56:27
Django
Ruslan Bergutov, 2015-05-15 07:56:27

How to set django loop limit in tags?

There is a mini-feed with news, you need to limit the number of news to 5

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Kitaev, 2015-05-15
@deliro

If you need a paginator:

from django.views.generic import ListView

class NewsList(ListView):
    model = News
    paginate_by = 5

If you just throw out the last 5:
It's better not to put it in tags. But if you really want to do it in a template:
class News(Model):
    # Твоя модель новостей
    @classmethod
    def last_five(cls):
        return cls.objects.all()[:5]

Then pass the model to the context:
And in the template write:
<div class="my-very-own-news">
    {% for news in News.last_five %}
        {{ news }}
    {% endfor %}
</div>

Z
zelsky, 2015-05-15
@zelsky

Here is an example.
news = AllNews.objects.order_by('pk')[0:5]

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question