Asinox
Asinox

Reputation: 6865

Django, The {% if %} tag evaluates

Well, im out my mind of what jeje .... how ill evaluate a variable with {% if %} in template?

im trying this

{% if variable > 0 %}  or {% if variable != 0 %}

but...i got errors :(

Thanks

Upvotes: 1

Views: 1563

Answers (4)

Daniel  Magnusson
Daniel Magnusson

Reputation: 9674

What django are you using? 1.2 and above have smart if https://docs.djangoproject.com/en/dev/releases/1.2/

You can now do this:

{% if a != b  || c != d %}
 ...
{% endif %}

Upvotes: 0

Daniel Roseman
Daniel Roseman

Reputation: 599480

Instead of continually posting here every time you have a minor problem, you should read Django's excellent documentation. In particular, the template docs have a very good explanation of why what you have tried is not allowed.

In this case, you could benefit from the fact that in Python 0 is the same as False. So you could have

{% if variable %}

or

{% if not variable %}

Otherwise, use {% ifequal %} and {% ifnotequal %} as MaRiz explains.

Upvotes: 1

Wade
Wade

Reputation: 1065

Check out Django smart-if template tag http://simonwillison.net/2009/Mar/3/smartif/ http://www.djangosnippets.org/snippets/1350/

It replaces the built-in if tag and lets you do comparison operators.

Upvotes: 3

AbstractProblemFactory
AbstractProblemFactory

Reputation: 9811

You should use

{% ifnotequal... 
{% ifequal... 

etc :)

Here is all: http://docs.djangoproject.com/en/dev/topics/templates/#topics-templates

Upvotes: 1

Related Questions