Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

last row of a queryset in django template

I have a query set in Django template. It has a price column. I want to display only the price of last row. Is there any template filter which will help the cause?

like image 387
Subodh Deoli Avatar asked Oct 31 '25 10:10

Subodh Deoli


1 Answers

Those who suggust using filter {{ queryset | last }} are not right. You'll get 'Negative indexing is not supported' this way.

So, the only way seems writing custom filter or use an ugly construction like:

{% for obj in queryset %}
    {% if forloop.last %}
        {{ obj.price }}
    {% endif %}
{% endfor %}

UPD: According to Django changelog {{ queryset.last }} will work from 1.6

like image 61
I S Avatar answered Nov 02 '25 23:11

I S



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!