I'm trying to run this code in my django template:
{% if lawChoice.releveAnnee==law.releveAnnee %}
I get the following error:
Could not parse the remainder: '==law.releveAnnee' from 'lawChoice.releveAnnee==law.releveAnnee'
I think law.releveAnnee is considered as an object, not as an integer, that's why the comparison fails!
Thanks in advance for your help,
Romain
EDIT
Thanks to the answer above: a SPACE is needed BEFORE and AFTER the ==:
{% if lawChoice.releveAnnee == law.releveAnnee %}
This works! Solved :)
Probably that's because you need to add a space before and after the == operator.
You'd better go for ifequal
{% ifequal lawChoice.releveAnnee law.releveAnnee %}
...
{% endifequal %}
Related
I have tried the following without success:
{% if forloop.counter % 2 == 0 %}
Does anyone have any suggestions that might help me achieve my aim? Any help is much appreciated!
you can try this way
{% if forloop.counter|divisibleby:"2" %}
https://docs.djangoproject.com/en/2.2/ref/templates/builtins/#divisibleby
The built-in filter divisibleby can be used instead of the modulus operator
{% if forloop.counter|divisibleby:"2" %}
Use this
{% if forloop.counter|divisibleby:"2" %}
I'm getting a strange error. Putting {% if any([None, 1]) %}show{% endif %} in my Django template creates a TemplateSyntaxError.
Experimentation shows that {% if True %}show{% endif %} works so I know the problem is from any([None, 1]). I checked that any([None, 1]) in python outside of Django and it returned True as expected so my code seems like it should work in the template. What am I doing wrong?
Thank you for you time.
I think you should use proper boolean operators with {% if %} Django built-in:
https://docs.djangoproject.com/en/3.0/ref/templates/builtins/#boolean-operators
Or write your own templatag as suggested by Nafees Anwar
I am using this code in my templatetags:
http://pastie.org/3530409
And I know for context problem and bad design (that this logic should not be in view) but I need in template solution for this:
{% for tag in page.tagname_list %}
{% ifequal tag "wiki" %}
{% set howto = 1 %}
{% endifequal %}
{% endfor %}
So I can use howto variable latter for my view logic.
Is there any way to do this in view templates, without model modification ?
If answer yes, please provide some solution...
Thanks a lot.
Instead of having to set the variable, you could just do:
{% if "wiki" in page.tagname_list %}
# do your wiki stuff below.
{% endif %}
Is it possible to create a Django template tag which evaluates to a boolean?
Eg, can I do:
{% if my_custom_tag %}
..
{% else %}
..
{% endif %}
At the moment I've written it as an as tag, which works fine like this:
{% my_custom_tag as var_storing_result %}
But I was just curious if I could do it the other way as I think it'd be nicer if I didn't have to assign the result to a variable first.
Thanks!
Actually.. what you can do is register tag as assignment_tag instead of simple_tag Then in your template you can just do {% my_custom_tag as var_storing_result %} one time and then regular if blocks where ever you want to evaluate the boolean. Super useful! For example
Template Tag
def my_custom_boolean_filter:
return True
register.assignment_tag(my_custom_boolean_filter)
Template
{% my_custom_boolean_filter as my_custom_boolean_filter %}
{% if my_custom_boolean_filter %}
<p>Everything is awesome!</p>
{% endif %}
Assignment Tag Official Doc
One alternative might be to define a custom filter that returns a boolean:
{% if my_variable|my_custom_boolean_filter %}
but that will only work if your tag depends on some other template variable.
You'd have to write a custom {% if %} tag of some sort to handle that. In my opinion, it's best to use what you already have in place. It works well, and is easy for any other developers to figure out what's going on.
Is it possible to use this code
{% if item.status == "0" %}
<td>Pending</td>
{% else %}
<td>Approved</td>
{% endif %}
if the item.status is an integer?
It seems to never go into the if statement and prints else all the time
Should i declare the variable first? eg something = item.status?
If yes then what is the correct syntax?
Remove the quotes around the 0 and it looks like it should work. See here
You are comparing it to a string "0", not am integer 0. That's the problem. The syntax is fine, just remove the quotes.
this should work.
{% ifequal item.status 0 %}
<td>Pending</td>
{% else %}
<td>Approved</td>
{% endifequal %}
Edit
just to clarify (as the other answers have mentioned) the issue is "0", comparing int == string, ifequal is just my preferred way of using the template tag.