Index in templates of django is like this:
{{somearray.i}}
for my code this is not working!!
this is views.py
def fadakpage(request):
tours = tour.objects.order_by('tourleader')
travelers = traveler.objects.order_by('touri')
j=0
for i in tours:
j+=1
args={'tours':tours,'travelers':travelers,'range':range(j)}
return render(request,'zudipay/fadakpage.html',args)
this is fadakpage.html / template (it shows empty):
{% for i in range %}
{{tours.i.tourleader}}
{% endfor %}
but if i change {{tours.i.tourleader}} to {{tours.0.tourleader}} it works!!
I also checked I values and it was true !!
Not sure if this is exactly what you need. You can get the loop counter by using {{ forloop.counter }} to get the loop index starting at 1, or {{ forloop.counter0 }} to get the index starting at 0.
{% for tour in tours %}
{{ tour.tourleader }} {{ forloop.counter }}
{% endfor %}
See the docs for more info.
No, indeed, that does not work in a Django template. But there is no reason to do it: just loop through tours.
{% for tour in tours %}
{{tour.tourleader}}
{% endfor %}
You change your view to this:
def fadakpage(request):
j = 0
tours = []
for i in tour.objects.order_by('tourleader'):
tours.append((i, j))
j += 1
args = {'tours': tours, 'range': range(j)}
return render(request, 'zudipay/fadakpage.html', args)
And use list of tuples in your template:
{% for tour in tours %}
{{ tour.0.tourleader }}
{% endfor %}
In this code in your template {{ tour.0 }} is tour object and {{ tour.1 }} is count.
Related
I want to do something like the following but do not know how to make it compatible with the HTML that I've written for my program.
{% for u in sorted(dictionary, key=dictionary.get, reverse=True) %}
{{ u|safe }}<br>
Priority level <b>{{ dictionary[u] }}</b><br>
---<br>
{% endfor %}
I'm not opposed to doing it in the views.py file either. Ideally I'd like to only show the top 20 or so results as well.
Sort in the view, as you are doing, and also reverse the order and limit to 20:
return render(request, 'patrec.html', {
'siglist': sorted(siglist.iteritems(), reverse=True)[:20],
'best_match': best_match,
})
And then access the sorted items in the template:
{% for tup in siglist %}
<p> Key is {{ tup[0] }} </p>
<p> Value is {{ tup[1] }} </p>
{% endfor %}
you can try inbuilt dictsort to sort your dictionary https://docs.djangoproject.com/es/1.10/ref/templates/builtins/#dictsort
I tried everything from everywhere to no avail. I came up with the following-
sort = []
for u in sorted(siglist, key=siglist.get, reverse=True)[:25]:
sort.append([u, str(siglist[u])])
return render(request, 'patrec.html', {
'siglist': sort,
'best_match': best_match,
})
And then put the following in the HTML-
{% for u in siglist %}
{{ u.0 }}<br>
{{ u.1 }}<br><br>
{% endfor %}
Let's say that I have a code like:
{% for x in posts %}
<p>We are in item: {{ x }}</>
{% else %}
And I want to get the current index of the for loop to run an if loop, something like(logically):
{% for x in posts %}
{% if x.index = 0 %}
<p>We are in the first item!</p>
{% else %}
<p>We are in item: {{ x }}</>
{% endif %}
{% endfor %}
How to do it inside jijna2? (I use Python with Flask).
You can use loop.index inside the loop.
{% for x in posts %}
<p>We are in item number: {{ loop.index }}</>
{% else %}
Flask Template docs
You can use several loop related variables for that
http://jinja.pocoo.org/docs/dev/templates/#list-of-control-structures
I am using Flask and I have a WTF form with 12 input fields named like sold_1, sold_2,..., sold_12.
I would like to generate these fields in Jinja using a loop like:
{% for r in range(1, 13) %}
{{ form.sold_ }}{{ r }}
{% endfor %}
or a similar syntax, but it doesn't work.
I solved it in a quite convoluted way as follows:
{% set tmp = "sold_x" %}
{% for r in range(1, 13) %}
{{ form[tmp | replace('x', r)] }}
{% endfor %}
but I wonder whether there is a cleaner way.
Thanks
You could use this:
{% for r in range(1, 13) %}
{{ form.sold_ ~ r }}
{% endfor %}
or, if you want your input fields names to be sold_nr:
{% for r in range(1, 13) %}
{{ 'sold_' ~ r }}
{% endfor %}
See this answer for more detail.
EDIT
Using the #dirn and #Libra sugestions the correct answer is:
{% for r in range(1, 13) %}
{{ form['sold_' ~ r] }}
{% endfor %}
I am getting 2 querysets from db:
all_locations = Locations.objects.all()[:5]
rating = Rating.objects.all()[:5]
return render_to_response('index.html',{'all':all_locations,'rating':rating},context_instance=RequestContext(request))
But I am stuck here, not knowing how to loop over these 2 querysets in one loop. this is being wrong:
{% if all and rating %}
{% for every in all and rating %}
{{every.locationname}}, {{every.rating_score}}
{% endfor %}
{% endif %}
You can try zip(all_locations, rating). It will produce a list of tuples. Then you can iterate over them in pairs. Here is an example: (demo)
all_locations = ['ca','ny','fl']
ratings = ['best','great','good']
for (l,r) in zip(all_locations,ratings):
print l+':'+r
Outputs
ca:best
ny:great
fl:good
I have also come across this problem. Now I've fixed it.
what I do is using
new=tuple(zip(queryset1,queryset2))
return render(request, 'template.html', {"n": new}).
in view.py.
In template.html, I use three for sentences which are list below.
{% for i in n %}
{% for j in i|slice:"0:1" %}
......operate queryset1
{% endfor %}
{% for z in i|slice:"1:2" %}
.....operate queryset2
{% endfor %}
{% endfor %}
It seems this method will fulfill your needs.
this might work:
{% with rating|length as range %}
{% for _ in range %}
{{ rating[forloop.counter] }}
{{ location[forloop.counter] }}
{% endfor %}
{% endwith %}
i'm not sure if rating|length will to the job... you might need to add rating|length|times' withtimes` filter defined as:
#register.filter(name='times')
def times(number):
return range(number)
I would like to do something like:
variable p is from test.py which is a list ['a','b','c','d']
{% for i in p %}
{{variable++}}
{{variable}}
result output is:
1 2 3 4
You could use loop.index:
{% for i in p %}
{{ loop.index }}
{% endfor %}
Check the template designer documentation.
In more recent versions, due to scoping rules, the following would not work:
{% set count = 1 %}
{% for i in p %}
{{ count }}
{% set count = count + 1 %}
{% endfor %}
After 2.10, to solve the scope problem, you can do something like this:
{% set count = namespace(value=0) %}
{% for i in p %}
{{ count.value }}
{% set count.value = count.value + 1 %}
{% endfor %}
As Jeroen says there are scoping issues: if you set 'count' outside the loop, you can't modify it inside the loop.
You can defeat this behavior by using an object rather than a scalar for 'count':
{% set count = [1] %}
You can now manipulate count inside a forloop or even an %include%. Here's how I increment count (yes, it's kludgy but oh well):
{% if count.append(count.pop() + 1) %}{% endif %} {# increment count by 1 #}
Or...
{% set count = [] %}
{% for something-that-loops %}
{% set __ = count.append(1) %}
<div> Lorem ipsum meepzip dolor...
{{ count|length }}
</div>
{% endfor %}
(From comments by #eyettea and #PYB)
Here's my solution:
Put all the counters in a dictionary:
{% set counter = {
'counter1': 0,
'counter2': 0,
'etc': 0,
} %}
Define a macro to increment them easily:
{% macro increment(dct, key, inc=1)%}
{% if dct.update({key: dct[key] + inc}) %} {% endif %}
{% endmacro %}
Now, whenever you want to increment the 'counter1' counter, just do:
{{ increment(counter, 'counter1') }}
if anyone want to add a value inside loop then you can use this its working 100%
{% set ftotal= {'total': 0} %}
{%- for pe in payment_entry -%}
{% if ftotal.update({'total': ftotal.total + 5}) %}{% endif %}
{%- endfor -%}
{{ftotal.total}}
output = 5
Came searching for Django's way of doing this and found this post. Maybe someone else need the django solution who come here.
{% for item in item_list %}
{{ forloop.counter }} {# starting index 1 #}
{{ forloop.counter0 }} {# starting index 0 #}
{# do your stuff #}
{% endfor %}
Read more here:
https://docs.djangoproject.com/en/1.11/ref/templates/builtins/
I was struggle with this behavior too. I wanted to change div class in jinja based on counter. I was surprised that pythonic way did not work. Following code was reseting my counter on each iteration, so I had only red class.
{% if sloupec3: %}
{% set counter = 1 %}
{% for row in sloupec3: %}
{% if counter == 3 %}
{% set counter = 1 %}
{% endif %}
{% if counter == 1: %}
<div class="red"> some red div </div>
{% endif %}
{% if counter == 2: %}
<div class="gray"> some gray div </div>
{% endif %}
{% set counter = counter + 1 %}
{% endfor %}
{% endif %}
I used loop.index like this and it works:
{% if sloupec3: %}
{% for row in sloupec3: %}
{% if loop.index % 2 == 1: %}
<div class="red"> some red div </div>
{% endif %}
{% if loop.index % 2 == 0: %}
<div class="gray"> some gray div </div>
{% endif %}
{% endfor %}
{% endif %}
Just to shed more light into this problem.
Jinja2 variables behaves differently from that of conventional scripting languages, you can't modify the variable in a for loop.Hence to bypass this behaviour you can use a dictionary, since you can change the value of the dictionary.
**{% set margin={"margin_value":0} %}**
{% for lang in language %}
<ul>
<li style="margin-right: {{ margin.margin_value}}px">{{ lang }}</li>
</ul>
**{% if margin.update({"margin_value":margin.margin_value + 2}) %}
{% endif %}**
{% endfor %}
In the above code the value of the dictionary is being modified.