I have a ListView that sends context as news
in the HTML template.
The below code is supposed to slice the context array from the 4th element to until its end
{% for post in news[4:0] %}
............
{% endfor %}
But following error occurs
Could not parse the remainder: '[4:]' from 'news[4:]'
CodePudding user response:
You can use the slice template tag:
{% for post in news|slice:"4:" %}
............
{% endfor %}
CodePudding user response:
You can do this inside your ListView by overriding the context data like this:
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context['last_4_news_items'] = News.objects.all().order_by('-date')[:4]
return context
template:
{% for news in last_4_news_items %}
{{ news }}
{% endfor %}