For example I have 5 row with data in my db and when I want to show first 3 row in my html,
I'm using for
loop with slice
like {% for x in y|slice:":3" %}
. But now my question is how to show last 3 row from db.
I would really advice you not to slice querysets in a template, but in the view. Normally a view decides what to show in the response, and a template decide how to show that in the response.
You can slice a queryset in the response with .reverse()[:3]
. So if your view has a queryset:
SomeModel.objects.all()
you can slice the queryset with:
SomeModel.objects.all().reverse()[:3]