51๐
Have a look at the {% empty %} tag.
Example from the documentation
<ul>
{% for athlete in athlete_list %}
<li>{{ athlete.name }}</li>
{% empty %}
<li>Sorry, no athletes in this list.</li>
{% endfor %}
</ul>
Link: https://docs.djangoproject.com/en/1.8/ref/templates/builtins/#for-empty
If you are interested in a table, or some kind of heading if there are results, add the forloop.first
:
-
{% for athlete in athlete_list %}
- {{ athlete.name }}
- Sorry, no athletes in this list.
{% if forloop.first %}
Athlete Name:
{% endif %}
{% empty %}
{% endfor %}
- [Django]-Using window functions in an update statement
- [Django]-Query for top x elements in Django
- [Django]-Python 3 list(dictionary.keys()) raises error. What am I doing wrong?
8๐
Itโs unfortunate that youโre stuck using a raw query set โ theyโre missing a lot of useful behavior.
You could convert the raw query set into a list in the view:
notes_as_list = list(notes)
return render_to_response(template, {"notes":notes_as_list},context_instance=RequestContext(request))
Then check it as a boolean in the template:
{% if notes %}
Header
{% for note in notes %}
{{ note.text }}
{% endfor %}
{% endif %}
You could also make it happen without conversions using forloop.first
:
{% for note in notes %}
{% if forloop.first %}
Header
{% endif %}
{{ note.text }}
{% endfor %}
- [Django]-Django: How to set a field to NULL?
- [Django]-Django: How to get related objects of a queryset?
- [Django]-GeoDjango GEOSException error
6๐
In your view check whether notes
is empty or not. If it is then you pass None
instead:
{"notes": None}
In your template you use {% if notes %}
as normal.
- [Django]-Is there a datetime ยฑ infinity?
- [Django]-Tying in to Django Admin's Model History
- [Django]-How to call function that takes an argument in a Django template?
4๐
What about:
{% if notes != None %}
{% if notes %}
NOTES:
{% for note in notes %}
{{ note.text }}
{% endfor %}
{% endif %}
{% else %}
NO NOTES AT ALL
{% endif %}
- [Django]-Pre-populate an inline FormSet?
- [Django]-Django: import auth user to the model
- [Django]-Django: allow line break from textarea input
3๐
Your original solution
{% if notes %}
Header
{% for note in notes %}
{{ note.text }}
{% endfor %}
{% endif %}
Works now with Django 1.7 and thanks to QuerySet caching, it does not cost and extra query.
- [Django]-How to spread django unit tests over multiple files?
- [Django]-Django prefetch_related with limit
- [Django]-How to reset the sequence for IDs on PostgreSQL tables
3๐
Often the right way to do this is to use the {% with ... %}
tag. This caches the query so it runs only once and also gives you more flexibility with your markup than using {% empty %}
.
{% with notes as my_notes %}
{% if my_notes %}
<ul>
{% for note in my_notes %}
<li>{{ note }}</li>
{% endfor %}
</ul>
{% else %}
<p>Sorry, no notes available</p>
{% endif %}
{% endwith %}
With this particular example Iโm not sure how useful it is but if youโre querying Many-to-Many field, for instance, itโs likely what you want to do.
- [Django]-Can I have a Django form without Model
- [Django]-Add request.GET variable using django.shortcuts.redirect
- [Django]-How do you configure Django to send mail through Postfix?
1๐
Use {% empty %} in django templates
{% if list_data %}
{% for data in list_data %}
{{ data.field_1 }}
{% endfor %}
{% else %}
<p>No data found!</p>
{% endif %}
We can write above code with {% empty %}.
{% for data in list_data %}
{{ data.field_1 }}
{% empty %}
<p>No data found!</p>
{% endfor %}
- [Django]-Accepting email address as username in Django
- [Django]-Save base64 image in django file field
- [Django]-How do I raise a Response Forbidden in django
0๐
you can use the {% empty %}
for a "for loop" as in:
{% for object in list %}
<p>{{ object }}</p>
{% empty %}
<p>list is empty</p>
{% endfor %}
use โ.allโ
{% if object.something.all %}
- [Django]-Class views in Django
- [Django]-Django: how does manytomanyfield with through appear in admin?
- [Django]-Django Passing data between views