i've got an appengine project and in my template i want to do something like
{% for i in range(0, len(somelist)) %}
{{ somelist[i] }} {{ otherlist[i] }}
{% endfor %}
i've tried using 'forloop.counter' to access list items too, but that didn't work out either. any suggestions?
regards, mux
What you might want to do instead is change the data that you're passing in to the template so that somelist and otherlist are zipped together into a single list:
combined_list = zip(somelist, otherlist)
...
{% for item in combined_list %}
{{ item.0 }} {{ item.1 }}
{% endfor %}
Related
First of all, let me show our views.py file.
context = {
'id' : id,
'durum' : durum,
'range': range(len(id)),
}
I have such data in template;
context.id = [12, 10, 10]
context.durum = ['UPL','PPL','FIUPL']
I want to match this data like this;
12 UPL
10 PPL
10 FIUPL
I created a for loop for this, but need to edit
{% for i in context.range %}
{{ context.id }}
{{ context.durum }}
{% endfor %}
Like this;
{% for i in context.range %}
{{ context.id.i }}
{{ context.durum.i }}
{% endfor %}
But I can't use the variable i in the loop.
Use zip in view
Ex:
context = {
'data' : zip(id, durum)
}
And then in template
Use:
{% for id, durum in data %}
{{ id }}
{{ durum }}
{% endfor %}
You can use list comprehension.
So, for example:
my_list = list(zip(context['id'], context['durum'], context['range']))
And then in the template, you can use:
{% for item in my_list %}
{{ item.0 }} -- { item.1 }}
{% endfor %}
Well, it seems like you moved to python from some other language. You don't usually use indexing in python for loops (they are much easier and more intuitive) that's why it was hard for you to 'pair' those values. If you still can refactor your code, instead of making to list with attributes on matching indexes use a dict. One - it will let you unpack really easily in django template
{% for id, durum in my_dict %}
{{ id }} {{ durum }}
{% endfor %}
Two - it will prevent any errors connected with wrong index because you just call id and it will get the right durum. Three - it will be very easy to update such data set.
my_dict.update({new_id: new_durum})
Please consider spending a little bit of time learning new stuff because it will make your python experience much more pleasureable. Oh and btw - most of time you dont have to specify the {{ context.something }} call - its enough to call {{ something }}
My context dictionary for my Django template is something like the following:
{'key1':'1',
'key2':'2',
'key3':'3',
'key4':{'key5':{'key6':'6', 'key7':'7', 'key8':'8'}}}
I would like to iterate through the dictionary and print something like:
some label = 6
some label = 7
some label = 8
How can I achieve this in my Django template?
What's wrong with this ?
<ul>
{% for key, value in key4.key5.items %}
<li>{{ key }} : {{ value }}</li>
{% endfor %}
</ul>
NB: you didn't ask for looping over all keys in the context, just about accessing key4['key5'] content. if this wasn't wath you were asking for pleasit eadit your question to make it clearer ;-)
I am guessing you want to use a for loop in the django template to do this you must first pass the dictionary to the template in the views file like so make sure you add square brackets around the dictionary like this:
data = [{'key1':'1',
'key2':'2',
'key3':'3',
'key4':{'key5':{'key6':'6', 'key7':'7', 'key8':'8'}}
}]
return render(request,'name of template',{'data':data})
then in the html template:
{% for i in data%}
<p>{{i.key1}}</p>
<p>{{i.key2}}</p>
<p>{{i.key3}}</p>
<p>{{i.key4.key5.key6}}</p>
{% endfor %}
Now when you do the for loop you can access all the iteams in key4 like i have above when I put {{i.key4.key5.key6}}
Here is the docs for the for loop in django templates https://docs.djangoproject.com/en/3.0/ref/templates/builtins/
I am assuming thats what you want to do.
This has worked for me:
{% for key, value in context.items %}
{% ifequal key "key4" %}
{% for k, v in value.items %}
some label = {{ v.key6 }}
some label = {{ v.key7 }}
some label = {{ v.key8 }}
{% endfor %}
{% endif %}
{% endfor %}
If you want to print only what you have mentioned in question then it is possible but if we don't know the exact structure of dictionary then it is possible in django views not in django template.
You can't print value which is also a dictionary one by one in django template,
But you can do this in django view.
Check this post click here and do some changes in views.
I have the following thing in a jinja2 for loop:
{{ meal[item]['open-modal'].submit(**{ 'class':'btn btn-primary',
'data-toggle':'modal',
'data-target':'#myModal' }) }}
I need to have an index on the data-target like:
{{ meal[item]['open-modal'].submit(**{ 'class':'btn btn-primary',
'data-toggle':'modal',
'data-target':'#myModal-item' }) }}
item is the index needed in this case. Is there a way to escape item out of this "ad-hoc dictionary"? So that it takes on the same values as in meal[item]?
I need the 'data-target' attribute to render as '#myModal-0', '#myModal-1', etc.. As it stands each 'data-target' attribute gets set as '#myModal-item' for each item in the loop. In other words it sets item in the second line of code as a string.
In case it is ever helpful for someone, what wound up solving my problem was:
<form method="POST">
{{ meal[item]['open-modal'].csrf_token }}
{{ meal[item]['open-modal'].submit( **{ 'class':'btn btn-primary',
'data-toggle':'modal',
'data-target':'#myModal-' +
item|string } ) }}
</form>
Keep in mind that this is nested inside of two for loops in jinja2.
{% for meal in menu_dict %}
{% for item in meal %}
....
{% endfor %}
{% endfor %}
The point is summarized with this, basically:
'data-target':'#myModal-' + item|string
adds the postfix.
i am new to django and tried to use the variable of an loop as a key for another object:
this is my views.py
...
files = Files.objects.filter(dataset__id = instance.dataset.id)
context = {'files': files, 'range': range(files.count())}
return render_to_response("test.html", context, context_instance=RequestContext(request))
and my test.html looks like this:
{% for i in range %}
<img title="{{i}}" src="{{files.i.data_files.url}}"/>
{% endfor %}
if i use files.0.data_files.url (or 1,2,3..) instead of files.i. it works, but i want to give out all images and also need the position of the image.
can you help me please?
the thing you're probably looking for is a magic variable {{ forloop.counter }} (or {{ forloop.counter0 }} if you want to use zero-based index) available inside {% for %} loop:
{% for file in files %}
<img title="{{ forloop.counter }}" src="{{file.data_files.url}}"/>
{% endfor %}
i have the following template namely index.html.I am working in django framework.Now i have shown the value where the all speed of a car is more than 30km.I just posted here the part of the associated code which output the value,that is,the code is
{% for v in values %}
{% if v.speed > 30 %}
{{v.speed}}
{% endif %}
{% endfor %}
now i just want to count the v.speed values,how can i do that using python or django count function.You can edit my code in that section.
Thank You.
If values is a Django QuerySet you can just use .count()
{% for v in values %}
{% if v.speed > 30 %}
{{v.speed}}
{% endif %}
{% endfor %}
{{values.count}}
You can also use {{values|length}}
Variable assignment is not allowed in django. So, your only alternative is do the counting in python itself & pass the corresponding data to the template.
Thus, you should do the following in python
speedy_values = [v for v in values if v.speed > 30]
And then, pass the speedy_values to your template
{% for v in speedy_values %}
{{v.speed}}
{% endfor %}
{{ v|length }} number of cars have speed greater than 30.
Alternatively, since your QuerySet has been already evaluated, using the length filter would save you an additional query:
{% for v in values %}
{% if v.speed > 30 %}
{{v.speed}}
{% endif %}
{% endfor %}
{{values|length}}
Documentation.