377👍
See https://docs.djangoproject.com/en/stable/ref/templates/builtins/#if : just use, to reproduce their example:
{% if athlete_list %}
Number of athletes: {{ athlete_list|length }}
{% else %}
No athletes.
{% endif %}
130👍
If you’re using a recent Django, changelist 9530 introduced an {% empty %} block, allowing you to write
{% for athlete in athlete_list %}
...
{% empty %}
No athletes
{% endfor %}
Useful when the something that you want to do involves special treatment for lists that might be empty.
- [Django]-Logging in Django and gunicorn
- [Django]-Passing variable urlname to url tag in django template
- [Django]-Django – How to rename a model field using South?
28👍
A list is considered to be False
if it has no elements, so you can do something like this:
{% if mylist %}
<p>I have a list!</p>
{% else %}
<p>I don't have a list!</p>
{% endif %}
- [Django]-How do you serialize a model instance in Django?
- [Django]-How to merge consecutive database migrations in django 1.9+?
- [Django]-Is this the right way to do dependency injection in Django?
13👍
If you tried myList|length and myList|length_is and its not getting desired results, then you should use myList.count
- [Django]-Django rest framework: query parameters in detail_route
- [Django]-Django set default form values
- [Django]-Running a specific test case in Django when your app has a tests directory
9👍
You can try with:
{% if theList.object_list.count > 0 %}
blah, blah...
{% else %}
blah, blah....
{% endif %}
- [Django]-How to force Django models to be released from memory
- [Django]-Timestamp fields in django
- [Django]-How to mix queryset results?
8👍
This works:
{% if myList|length %}
Do something!
{% endif %}
Why there’s so many answers here and why there’s so much confusion is that this didn’t always work. I think at one point template filters couldn’t be used on arguments to the if
statement and this was later added. It’s also now possible to do things such as {% if myList|length >= 3 %}
. The filter should do the equivalent of len(myList)
so any type of object that can handle that would also be able to handle the |length
filter.
- [Django]-Django Rest Framework Conditional Field on Serializer
- [Django]-How to set the timezone in Django
- [Django]-How can I use the variables from "views.py" in JavasScript, "<script></script>" in a Django template?
1👍
Collection.count no bracket
{% if request.user.is_authenticated %}
{{wishlists.count}}
{% else %}0{% endif %}
- [Django]-Table thumbnail_kvstore doesn't exist
- [Django]-How does Django's nested Meta class work?
- [Django]-In a django model custom save() method, how should you identify a new object?
1👍
I need the collection length to decide whether I should render table <thead></thead>
but don’t know why @Django 2.1.7 the chosen answer will fail(empty) my forloop
afterward.
I got to use {% if forloop.first %} {% endif %}
to overcome:
<table>
{% for record in service_list %}
{% if forloop.first %}
<thead>
<tr>
<th>日期</th>
</tr>
</thead>
{% endif %}
<tbody>
<tr>
<td>{{ record.date }}</td>
</tr>
{% endfor %}
</tbody>
</table>
- [Django]-Python Asyncio in Django View
- [Django]-Mixin common fields between serializers in Django Rest Framework
- [Django]-Django related_name for field clashes