django - show the length of a queryset in a template

In my html file, how can I output the size of the queryset that I am using (for my debugging purposes)

I've tried

{{ len(some_queryset) }}

but that didn't work. What is the format?


Give {{ some_queryset.count }} a try.

This is better than using len (which could be invoked with {{ some_queryset.__len__ }}) because it optimizes the SQL generated in the background to only retrieve the number of records instead of the records themselves.


some_queryset.count() or {{some_queryset.count}} in your template.

dont use len, it is much less efficient. The database should be doing that work. See the documentation about count().

However, taking buffer's advice into account, if you are planning to iterate over the records anyway, you might as well use len which will involve resolving the queryset and making the resulting rows resident in main memory - this wont go to waste because you will visit these rows anyway. It might actually be faster, depending on db connection latency, but you should always measure.