NewToJS
NewToJS

Reputation: 2101

Django template: handling the href attribute as a conditional

Right now in my Django template I'm writing a whole new a tag if the conditional passes or fails. Is there a way to write this conditional in the a tag so that there is only one a tag?

{% for app in apps %}
    {% if app.app_id == "app-smart" %} 
      <a href='{{app.url}}' class='portfolio-link'>
    {% else %}
      <a href='{% url app.url %}' class='portfolio-link'>
    {% endif %}
{% endfor %}

Upvotes: 0

Views: 1771

Answers (1)

bruno desthuilliers
bruno desthuilliers

Reputation: 77902

Quite simply:

{% for app in apps %}
  <a href='{% if app.app_id == "app-smart" %}{{app.url}}{% else %}{% url app.url %}{% endif %}' class='portfolio-link'>
{% endfor %}

There's nothing magic in django templates, that's just plain text templating.

Upvotes: 5

Related Questions