user8733268
user8733268

Reputation:

When Query returns Null (no objects) how to display "nothing found" in django template?

I'm using chain method for querying the database something like,

objects = chain(Data.objects.filter(q_obj)..... )

In template,

{% for obj in objects %}
    ...
{% endfor %}

But, if objects returns No data then how can I print out "Nothing found".

I tried all of these,

{% if objects == " " %}

{% for obj in objects %}

...

{% endfor %}

{% else %}

Nothing found

{% endif %}

Also I tried,

if objects == None

if objects is null

if objects|default_if_none:""

if not objects

if objects|length > 0

But couldn't make that happen.

Upvotes: 1

Views: 265

Answers (1)

dirkgroten
dirkgroten

Reputation: 20702

{% for object in objects %}
   ...
{% empty %}
   nothing found
{% endfor %}

Upvotes: 3

Related Questions