Comparison Between Variable And Loop Iterator In Template Always Failing - django

I am trying to print something whenever an int variable from one of my models == my iteration of the loop.
My iteration counter increments correctly, and the test passes if I change it to if data.number == '1'.
edit: I should mention that if data.number == 1 fails, which I think is strange.
I've read a lot of posts about this and I'm sure this should work, but it doesn't.
{% for iteration in range %}
{% for data in lesson_data.all %}
# this never passes
{% if data.number == iteration|add:"1" %}
Thank you.

Wow, I had to cast:
{% if data.number|add:0 == iteration|add:"1" %}

Related

How to use filters inside an if condition? - shopify liquid

Is it possible to use filters inside an if statement condition ?
I found no way of doing this unless I make a variable that stores the filtered data and then use it inside my condition. Seems odd to me, there must be a better way.
I want to do something like this but I am getting an error:
{% if numA | plus:5 >= numB %}
I want to avoid doing this:
{% assign temp = numA | plus:5 %}
{% if temp >= numB %}
What you are trying to do in not possible in Shopify Liquid. From the official Shopify Liquid issues page
Parenthesis aren't allowed in Liquid. They can't be used in
conditionals the way you would use them in a programming language.
Filters are not allowed in conditionals, they will lead to unexpected
results, at least in Shopify.
The following:
{% if cart.item_count|times:1 > 5 %}
Generates this Liquid warning:
Expected end_of_string but found pipe in "cart.item_count|times:1 > 5"
So, the only possible solution is what you suggested in your own question.
{% assign temp = numA | plus:5 %}
{% if temp >= numB %}
Math filters in IF condition - Liquid

Django Template For Loop Iterator Not Usable

In a Django template the following syntax works just fine:
{% for test in testing.1 %}
but when replacing the fixed 1 with a dynamic variable from an outer for loop it doesn't work at all:
{% for i in range %}
{% for test in testing.i %}
When printing {{ i }} it shows 1, 2 and 3 like it should. Do I have to convert the i-variable somehow? Or can't I use the iterator variable there?
As #svrw commented: a dictionary lookup can't use a variable as key. I had to prepare the data so that I could use
{% for element in range %}
{% for test in element %}
and get the correct output. My data in the views.py I changed from this:
range = {
1: data1,
2: data2,
3: data3
}
to:
data1 = [element1, element2, element3 ...]
...
range = [data1, data2, data3]

Django - Select from a list

I have a list :
logo_nofav = [('codwaw.png', 'codwaw'), ('lol.png', 'lol')]
In my template I can :
call the first in the list with {{logo_nofav.0}}
call the second with {{logo_nofav.1}}
just call the second element of the second in the list with {{logo_nofav.1.1}}
Now my question is, how could I just call the second element of each object.
We could image {{logo_nofav.x.1}} where "x" is a variable which means anywhere on the list.
I hop I was clear. Thanks
You could iterate through the list,
{% for item in logo_nofav %}
{{ item.1 }}
{% endfor %}

Could not parse the remainder for boolean operators in django template

How can I use a boolean operator in django template? I want to do something like:
{% if forloop.counter<=12 or forloop.counter>=25 %}
But it is giving me an error:
Could not parse the remainder: '<=12' from 'forloop.counter<=12'
Try this by keeping space around operator
{% if forloop.counter <= 12 or forloop.counter >= 25 %}
https://docs.djangoproject.com/es/1.10/ref/templates/builtins/#id4, i think the problem is that you forgot the space betwen the operator and the variable.
foorloop.counter <= 12
The parser used by Django templates fails to parse the values if there is a lack of whitespace around the operators. There was a ticket #27022 opened on Django's issue tracker but it has been marked as "wontfix" in an effort to enforce a consistent style in templates.
Hence you need to update your code and add spaces around the operators:
{% if forloop.counter <= 12 or forloop.counter >= 25 %}

Iteration number of for loop in a template

In a Django template, I want to create a for loop that iterates through a list. During that loop, I also want to be able to use the iteration number of the loop.
For example, if some_list has 4 elements, then:
{% for o in some_list %}
# Print out the iteration number
{% endfor %}
Should print out the following:
>> 0
>> 1
>> 2
>> 3
How can I do this?
See the docs for the for tag. To get the loop index use {{ forloop.counter0 }} inside the block.