Django - Select from a list - django

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

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

Comparison Between Variable And Loop Iterator In Template Always Failing

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

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]

Loop through an array django - slightly more complex

I am using a JQuery chart library where you pass values a format like [0,2,5,9].
I have an array in my views where I currently access each index to return the value rest 1 = arr[0], rest 2 = arr[1] ... and then passing these values from the view into my HTML page and inserting the values for the chart like [res1,res2]. This is not feasible because I never know the size of the array so it'd be a constant manual approach of accessing the array. Is there a way I easily loop through each one?
Slight problem. Currently after accessing each index I convert value to an int. So I don't think I'd be able to do it via looping through the html page - it'd have to be done via views. Unless I can somehow call conversion function defined in the view in the html page?
--- views ---
array
-> returns multiple string values i.e. name = paul, name = john
-> paul = arr[0]
-> function(paul['']) converts it to a string.
--- html page ---
{{paul}}
ideally i'd like to do:
[rather than refer to each index here just loop through all array values.. but somehow calling my conversion function too on each value else what I want to do won't work. ]
{% for values in array %}
[insert each one here and call the convert int function from views]
I might misunderstood your question, but sounds like you just want to pass a list of string to the template, loop on each item in the list and do some conversion. I'm not really sure which step did you get stuck but the simplest way is to do everything in the views.py:
views.py
def view_func(request):
array = [{'Speed': 2, 'Height': 1, 'PersonID': 1}, {'Speed': 2, 'Height': 1, 'PersonID': 1}]
# do the conversion on each value in the list
converted_array = [int(i['speed']]) for i in array]
context = {'array': array, 'converted_array': converted_array}
template:
<!-- to loop on original array -->
{% for value in array %}
{{ value }}
{% endfor %}
<!-- to loop on the converted array -->
{% for value in converted_array %}
{{ value }}
{% endfor %}

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.