84

this is my code :

{% for i,j in enumerate(a) %}
 {{i}} ,{{j}}
{% endfor%}

but , it show a error , i think it cant run the enumerate method ,

so how to run the enumerate in django template ,

thanks

asked Feb 16, 2011 at 6:53

5 Answers 5

127

The template subsystem has some special constructs built into the for/endfor block that allows you to access the current index of the loop without having to call enumerate.

{% for j in a %}
 {{ forloop.counter0 }}, {{ j }}
{% endfor %}

While this snippet solves your immediate problem, if you're expecting to have access to Python builtins and other Python constructs inside your Django templates, you may be misunderstanding the sandbox that it provides/enforces.

answered Feb 16, 2011 at 7:02
Sign up to request clarification or add additional context in comments.

1 Comment

Also, for everyone's quick reference: forloop.counter0 refers to 0-indexed counter while forloop.counter refers to 1-indexed counter
19

you can use {{ forloop.counter }} or {{ forloop.counter0 }} for the same effect, the latter is 0-indexed, thus more like enumerate.

answered Feb 16, 2011 at 7:02

Comments

11
{% for item in a %}
 {{ forloop.counter }}, {{ item }}
{% endfor %}

Link related

answered Feb 16, 2011 at 7:02

Comments

8

Django template makes up the presentation layer and are not meant for logic. From the docs

If you have a background in programming, or if you’re used to languages which mix programming code directly into HTML, you’ll want to bear in mind that the Django template system is not simply Python embedded into HTML. This is by design: the template system is meant to express presentation, not program logic.

Now to get the same functionality in Django, you will have to complete your logic in the views.

views.py

def my_view(request, ...):
 ....
 enumerated_a = enumerate(a);
 ....
 return render_to_response('my_template.html', {'enumerated_a ': enumerated_a }..)

Now enumerate function returns an enumerate object which is iterable.
my_template.html

{% for index, item in enumerated_a %}
 {{ index }},{{ item }}
{% endfor %}

Although I think you can probably change it to an enumerated list and use it like that as well.

answered Mar 4, 2014 at 11:10

2 Comments

I kinda liked this idea at first glance, but when I tried to use this on a formset it failed - giving Error with Management form. This could be probably overcome by more tweaking.
The issue was with your management form, not enumerate.
1

If however you need to use a function within a template, i suggest you create a filter or a tag instead. For reference, check out http://docs.djangoproject.com/en/1.2/howto/custom-template-tags/

answered Feb 16, 2011 at 9:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.