how to write query in Django Templates - django

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" %}

Related

django template forloop.counter multiple operations

I am trying to perform multiple mathematics operations on a forloop.counter in my Django template. Specifically I am trying to identify every 29th instance, after the 24th; so the 24th, 53rd, 82nd, 111th, 140th instances etc....
I have tried the following without success:
{% if forloop.counter == 24 or widthratio forloop.counter|add:"-24" 29 1 %}
{% if forloop.counter == 24 or forloop.counter|add:"-24"|divisibleby:29 %}
Does anyone have any suggestions that might help me acheive my aim? Any help is much appreciated!
You can write it like:
{% for element in collection %}
{% if forloop.counter|add:"-24"|divisibleby:"29" %}
...
{% endif %}
{% endfor %}
given the offset (here 24) is smaller than the period (here 29), this will work, otherwise we need an extra condition (for example with 31 and 29):
{% for element in collection %}
{% if forloop.counter >= 31 forloop.counter|add:"-31"|divisibleby:"29" %}
...
{% endif %}
{% endfor %}
For example:
>>> tp = '{% for element in collection %}{% if forloop.counter|add:"-24"|divisibleby:"29"%}A{% else %}B{% endif %}{% endfor %}'
>>> Template(tp).render(Context({'collection': range(100)}))
'BBBBBBBBBBBBBBBBBBBBBBBABBBBBBBBBBBBBBBBBBBBBBBBBBBBABBBBBBBBBBBBBBBBBBBBBBBBBBBBABBBBBBBBBBBBBBBBBB'
as you can see, for the iterations where the condition holds, the A is printed.
But in case you need to write complicated logic, in general you should reconsider the design, and look if you can not "move logic" to the view instead.
Another interesting option, besides Willem's, is to first write a modulo filter:
#register.filter
def modulo(num, val):
return num % val
And then in your template use the filter:
{% if forloop.counter|modulo:29 0 %}
However in general you can and should do such operations in your view, not your template. It's not clear why you're going about doing it this way, so you may have your reasons, but try to avoid it if you can.

django template convert object to int

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 %}

Django template - set variable in for loop

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 %}

Using increased/decreased variables in django templates

Maybe it's a little bit stupid question, but I didn't find an answer. Is there any way to use increased/decreased variables in django templates?
e.g.{{ some_variable + 1 }}
There's a built-in add filter:
{{ some_variable|add:"1" }}
One way of doing this is by using a django template filter.
https://docs.djangoproject.com/en/dev/howto/custom-template-tags/#writing-custom-template-filters
def inc(value):
return value+1
and then:
{{ some_variable|inc }}
Inside for loop use forloop.counter that will automatically increase the counter till the records.
{% for a in object_list %}
{{ forloop.counter }}
{% endfor %}

Django TemplateTag evaluating to a boolean

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.