[Django]-How to check if instance of model exists in django template

6👍

You can use the {% if %} tag. As Django doc says:

The {% if %} tag evaluates a variable, and if that variable is “true” (i.e. exists, is not empty, and is not a false boolean value) the contents of the block are output.

So you can do something like this:

{% if job %}

{{ job.title }}

{% else %}

<p>Hi from Uruguay</p>

{% endif %}

If you need this inside a for, as @dirkgroten said, you need to use the {% empty %} tag. There is an example in the Django doc.

Leave a comment