Looping through the length of a list in jinja2 - html

I'm looking to populate something similar in jinja in html.
for i in range(len(my_list)):
for j in my_list:
print(i,j)
I need both the i, j value as well and to not just iterate through the list.
Don't know if there is something equivalent to range in jinja2

You can use loop.index0 for the purpose:
{% for i in my_list %}
{{loop.index0}} {{i}}
{% endfor %}

Related

jinja2 - create href with variable from loop

I'm try to create a dynamic href for a website
I've tried this:
(where "gruppe" is a list of servers)
{%- for item in groups[gruppe] %}
{% set url = 'https://cmk.abc.local/abc/check_mk/view.py?host=' + {{ hostvars[item]['openstack']['name'] }} + '&site=abc&view_name=host' %}
{{ hostvars[item]['openstack']['name'] }}.abc.local
{% endfor %}
Expected result should be:
https://cmk.abc.local/abc/check_mk/view.py?host=server01&site=abc&view_name=host#
Does anyone have an idea what I'm doing wrong?
It's tough to say without knowing the shape of the data. If you can post what "groups" looks like (as JSON) that would be helpful.
The first thing that stands out to me is "gruppe". Is that supposed to be a key in the groups object or is that supposed to be dynamic?
Try:
{%- for item in groups["gruppe"] %}
...

How to set loop.index as a variable in Jinja

{% for data in data_list %}
{% set data_index = {{loop.index}} %}
for data_dict in data:
pass
In my inner loop, I need to use the loop index in the outer loop, so I intend to set it to a variable as above. But the syntax is invalid.
How to do that? Or is there another way to get the outer loop index?
i think, you should not use Expressions({{..}}) inside statements ({%..%}), try this :
{% for data in data_list %}
{% set data_index = loop.index %}
for data_dict in data:
pass
You could use the built-in enumerate function for the same to get i as the variable and also use it in an inner loop if you want.
{% for i,data in enumerate(data_list) %}
{{ i }}
{% for j in range(i) %}
{% endfor %}
{% endfor %}
All you need to do is pass enumerate or whatever built-in python function you need as a parameter to the render template function as shown below
#app.get("/foo")
def foo():
return render_template("foo.html", enumerate=enumerate, range=range)

How to increment two variables on a for-loop in jinja template?

I am developing a web using flask. I have two class objects from models.py. I need to loop over both of them at the same time in my HTML file using Jinja2.
For example:
I want to have the following code in jinja2 format:
for i,j in zip(items, team):
a= i+j
Want to convert it to jinja2 format:
{% for i,j in zip(items, teams) %}
{% a=i+j %}
{% endfor%}
What is the problem with this jinja2 code?
Jinja doesn't actually have a zip global function. So you need to make it available by doing:
app.jinja_env.globals.update(zip=zip)
Additionally, assignments require using the set keyword, e.g. {% set a = i + j %}
{% for i, j in items %}
{% set a = i + j %}
{% endfor %}
See also: "zip(list1, list2) in Jinja2?"
Thank you , it was very helpful. I just did this and worked:
in init.py file I add this:
app.jinja_env.filters['zip'] = zip
in the index.html:
{% for i, j in items | zip(teams) %}
{% set a = i + j %}
{% endfor %}

Accessing values of multiple lists using an index in Django template tags [duplicate]

I have two list objects of the same length with complementary data i want to render is there a way to render both at the same time ie.
{% for i,j in table, total %}
{{ i }}
{{ j }}
{% endfor %}
or something similar?
If both lists are of the same length, you can return zipped_data = zip(table, total) as template context in your view, which produces a list of 2-valued tuples.
Example:
>>> lst1 = ['a', 'b', 'c']
>>> lst2 = [1, 2, 3]
>>> zip(lst1, lst2)
[('a', 1), ('b', 2), ('c', 3)]
In your template, you can then write:
{% for i, j in zipped_data %}
{{ i }}, {{ j }}
{% endfor %}
Also, take a look at Django's documentation about the for template tag here. It mentions all possibilities that you have for using it including nice examples.
For any recent visitors to this question, forloop.parentloop can mimic the zipping of two lists together:
{% for a in list_a %}{% for b in list_b %}
{% if forloop.counter == forloop.parentloop.counter %}
{{a}} {{b}}
{% endif %}
{% endfor %}{% endfor %}
Use python's zip function and zip the 2 lists together.
In your view:
zip(table, list)
In your template, you can iterate this like a simple list, and use the .0 and .1 properties to access the data from table and list, respectively.
If it's just the variables i and j that you're looking at then this should work -
return render_to_response('results.html',
{'data': zip(table, list)})
{% for i, j in data %}
<tr>
<td> {{ i }}: </td> <td> {{ j }} </td>
</tr>
{% endfor %}
(credit to everyone else who answered this question)
Rather than using a dictionary (which does not guarantee any kind of sorting), use the python zip function on the two lists and pass it to the template.
You'll have to do this in the view - use the builtin zip function to make a list of tuples, then iterate over it in the template.
Template logic is purposely simple, anything even slightly complex should be done in the view.

Django template browse a table with variable

I have a query result on my views and i drop to my template, on my template i want to display values of my table, so i use
{{myTable.0}} and {{myTable.1}}
and i have the result of my query, but i want to display this values with a variable like {{myTable.x}} where x =0 to use in a loop for.
But it doesn't work, so if you have any solution.
Seems like myTable is a list an you want to iterate through this list. If so then you can use the {% for %} template tag:
{% for val in myTable %}
{{ val }}
{% endfor %}