Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OR condition in django templates

Is there a way to implement conditional OR inside templates? {% if %} would see if true or not...but what i'm looking for is to implement smthing when {% if %} OR {% if %}..thanks

like image 533
stackover Avatar asked Dec 29 '10 16:12

stackover


2 Answers

in Django 1.2, you can use OR inside an IF tag...see the built-in template tags

{% if var1 == 'val1' or var2 == 'val2' %}
like image 86
Ben ODay Avatar answered Oct 22 '22 10:10

Ben ODay


You can use the firstof template tag. It works similar to an "or":

var1 or var2

will use var1, if it is True or var2, if it is False. Exacly like this statement in the template:

{% firstof var1 var2 %}
like image 43
Sascha Avatar answered Oct 22 '22 09:10

Sascha