enumerate in templates

3,116 views
Skip to first unread message

Vicky

unread,
Dec 27, 2008, 6:20:06 AM12/27/08
to Django users
Is there a way to access the previous value of a for loop in
templates. Can anyone tell the template representation for the python
code like:

for i,j in enumerate(item):
...

Alex Koshelev

unread,
Dec 27, 2008, 6:59:28 AM12/27/08
to django...@googlegroups.com
{% for %} itself


--
---
Александр Кошелев
daev...@gmail.com

Daniel Roseman

unread,
Dec 28, 2008, 10:06:31 AM12/28/08
to Django users
I think the various forloop variables are what you want.

{% for j in item %}
{{ forloop.counter0 }}
{% endfor %}

See :
http://docs.djangoproject.com/en/dev/ref/templates/builtins/#for
--
DR.

Vicky

unread,
Dec 28, 2008, 11:21:33 PM12/28/08
to Django users
ya that's what i need :) thanks a lot :) :)

On Dec 28, 8:06 pm, Daniel Roseman <roseman.dan...@googlemail.com>
wrote:

Vicky

unread,
Dec 29, 2008, 4:02:19 AM12/29/08
to Django users
command like below is not working... can u suggest a way??


{% for node in nodes %}
{% ifequal nodes[(forloop.counter0)-1].node_type '2' %}

James Bennett

unread,
Dec 29, 2008, 4:11:36 AM12/29/08
to django...@googlegroups.com
On Mon, Dec 29, 2008 at 3:02 AM, Vicky <vicky...@gmail.com> wrote:
> command like below is not working... can u suggest a way??

It is very, very, very, very important to read the big warning at the
top of the template documentation, which states that the Django
template language *is not* just Python code embedded in HTML, and
which explains that only the specific things described in the
documentation will work. A large part of your problem seems to be that
you're ignoring this and simply dumping random Python expressions into
a template, when it's clearly stated that you can't do that (well, you
can, it just won't do anything useful).

You might want to take a step back and go through the template
documentation thoroughly, then think about how you're structuring the
data that's passed into your templates; in the overwhelming majority
of cases, attempts to put this much logic into a template (which is
meant to provide the bare minimum of logical constructs needed to
present data) imply a problem somewhere else in the code.


--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

Reply all
Reply to author
Forward
0 new messages