views:

159

answers:

3

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

A: 

You should use

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

etc :)

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

Rin
You just pointed to the overview of templates, the full tag and filter list is here: http://docs.djangoproject.com/en/dev/ref/templates/builtins/#ref-templates-builtins-tags
Wayne Koorts
the problem is that if i want this: {% ifnotequal variable 0 %} , but if variable is -1 then is "true" and i dont want that negative value will be "true". and the same for {% ifequal %}
Asinox
+4  A: 

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.

Wade
Wade thanks you, but im new with Django, and i dont know where ill put the code :(in the core, in my views, models, etc??i think that i need the snippet
Asinox
i did it, thanks for the snippet
Asinox
You could place it in the core, but that could cause problem if you update the django core.I have an app called "utils" that I store misc. routines and helpers I use across different projects. I just added the smart-if code to that app's templatetags folder.
Wade
Thanks Wade, the snippet is in my app :)Thanks again :)
Asinox
A: 

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.

Daniel Roseman
"Minor" is a matter of opinion. Believe it or not there are people in the world who are not as experienced as you are. It's all relative. Joel Spolsky has specifically stated that what he envisioned for this site was to have answers to all questions, even the most seemingly trivial. The fact that this has now been asked means it may not have to be asked again. You could have spent that time you used to complain at the start of your answer to instead use your editing privileges to improve his question.
Wayne Koorts
I think Joel Spolsky disagrees with your assertion, of "Dont post the question, if it is too simple, read the docs".
Lakshman Prasad
im sorry guys, the first think that i do is read the docs, believe it or not, but in the docs there is nothing about operators in {%if %}, i dont know if im wrong.who said that in my case -1 is true? i need 0 or >0 , but if i bring a value from database if the value is -1 so {% if variable %} is true.Im sorry.
Asinox