Reputation: 980
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?
Upvotes: 0
Views: 64
Reputation: 9359
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 %}
Upvotes: 1