Katsuya Obara
Katsuya Obara

Reputation: 963

Show url link only if condition meet using data in model - Django

I am using template to render basic html in django and specify link as below.

<li><a href="{% url 'project:geometry' object.pk %}">Geometry</a></li>  

I want to hide this link based on condition using model information.
Does anyone know how to do this?

Upvotes: 0

Views: 1202

Answers (2)

grrrrrr
grrrrrr

Reputation: 1425

You can surround the html in an if statement using the Django template language:

{% if object.something %}
    <li><a href="{% url 'project:geometry' object.pk %}">Geometry</a></li>  
{% endif %}

You can use operators, filters or complex expressions if object.something is not a boolean

Upvotes: 3

Sam Mason
Sam Mason

Reputation: 16174

Django has an if template tag, see:

https://docs.djangoproject.com/en/2.1/ref/templates/builtins/#if

from the docs:

{% if not athlete_list %}
  There are no athletes.
{% endif %}

Upvotes: 0

Related Questions