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.
Related
I'd like to check if there is a next value inside my for loop;
If so set a comma if not do nothing. Is there something like an {% ifnext %}?
Quick research took me to the following soloution. Assuming that we are in a forloop one could use:
{% if not forloop.last %},{% else %}{% endif %}
I know i can do this in django template
ifequal "this"
...//
else
...//
endifequal
but how do i write this
ifequal "this"
..//
elseifequal "this"
...//
else
....
is there a elegant way to achieve this in django ?
Django templates support the Python-like elif tag. For example:
{% if this == True %}
Yes!
{% elif this == False %}
No!
{% else %}
What?
{% endif %}
Source: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#if
The documentation is very easy to find, so I suggest you read up on that.
try this,
{% ifequal p.gender "M" %}
Bloke
{% else %}
Sheila
{% endifequal %}
The ifequal tag determines if two arguments are equal. The arguments can be either
variables or hard-coded strings. If the arguments match, the template engine renders the
code inside the ifequal block. You need to add an endifequal tag to end the block.
https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#ifequal
index.html
<td>{% if place or other_place or place_description%}{{ place}} {{ other_place}} {{place_description}}</td>
This is displaying all the data in template.I want to truncate the string if it is more than length 80.
Conditions are,
1.If place variable have more than 80 character,it should truncate their and need not show the other two variable like other_place and place_description.
2.If place variable and other_place variable making more than 80 character,in this case it should truncate from place_variable don't need to show place_description variable.
3.If all the three are their and the 80th character is made from place_description,need to truncate from their.
All fields are not mandatory,so whatever field is comes to display,it should show only 80 character.
Need help to do this.
Thanks
You could use slice for pre-django 1.4:
{% if place or other_place or place_description%}
{% with place|add:other_place|add:place_description as pl %}
{% if pl|length > 80 %}
{{pl|slice:80}}...
{% else %}
{{pl }}
{% endif %}
{% endwith %}
{% endif %}
If you are using django 1.4 or greater,
You can just use truncatechars
{% if place or other_place or place_description%}
{% with place|add:other_place|add:place_description as pl %}
{{pl|truncatechars:80}}
{% endwith %}
{% endif %}
You could probably do it with a combination of add/truncatechars e.g.
{{ place|add:other_place|add:place_description|truncatechars:80}}
You could also use 'cut' which is part of django template builtins
for example if
{{ file.pdf.name}}
gives 'store/pdfs/verma2010.pdf'
{{ file.pdf.name | cut:'store/pdfs/'}}
Would give 'verma2010.pdf'
Took me way too long to find the answer in 2022. It's truncatechars, e.g.
{{ my_string|truncatechars:1 }}
https://docs.djangoproject.com/en/3.2/ref/templates/builtins/#truncatechars
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 %}
I'd like to get the value of an element by key within an if statement.
Example:
works:
{{ example[5] }}
doesn't work:
{% if example2 is example[5] %} something ... {% endif %}
Exception thrown:
Unexpected token "punctuation" of value "[" ("end of statement block" expected)
Thank you very much
Kind regards
Instead of
{% if example2 is example[5] %} something ... {% endif %}
try changing 'is' to '=='
{% if example2 == example[5] %} something ... {% endif %}
Maybe you should use the attribute function to get the object or array value. This link may help you