but I'd like to be able to only take the first n items; in Python that would be
- python:
for item in all_items[:n]:
- jinja:
{% for i in items[:3] %}
{{ i }}\n
{% endfor %}")
findObjectsWithAttribute( haystack, attribute, value, function=None ) -> [...]
and/or
findObjectWithAttribute( haystack, attribute, value, function=None ) -> object or None
so it can be run like
findObjectWithAttribute( currentCourse.data.modules, 'path', page.pageURL, pageLink )
just the "findObjectsWithAttribute" variant because you can get the other application of it using something like
{% set blah = findObjectsWithAttribute( … ) %}
{% set result = blah[0] if len(blah) > 0 else None %}
Python
Hey all,
I've tried everything I can find online and I can't seem to get the length of a queryset from a template. I'm passing a queryset found using this:
Page.objects.filter(
Q(data__attributes__course__category__code__icontains=code)
).filter( data__attributes__course__live=True )
I can iterate over the dict (I think that's the type it is) just fine but I can't find the length, I've tried:
len(courses)
courses|length
courses.count
courses.size
Any ideas?
Thanks
Theo Julienne
12:30 PM (2 minutes ago)
to Adam, dev
Hey Adam,
You can call .count() on a QuerySet to get its length (this will only retrieve the length from the database, not the rows). Once you've iterated the QuerySet, .count() will return the result length without hitting the database again.
Cheers,
Theo
Hi Adam
In Django templates you have to use ".count" to call a function (it doesn't support () notation).. Does that give an error?
You can also use this syntax if you're doing what I think you're doing:
{% for item in queryset %} display the item {% empty %} you have no items! {% endfor %}
Cheers, Theo }}}