Numbering of items in template

45 views
Skip to first unread message

Jesse

unread,
Mar 17, 2009, 12:47:07 PM3/17/09
to Django users
Hello,

I've finally gotten pagination to work. Now I would like to add a
sequential number to the beginning of each record in the output. In
previous languages I used something like count=count+1 and then placed
count at the beginning of each record output in the template. How is
a record count created in django that can be displayed at the
beginning of each record in the template?

If I use <ol></ol> the count starts over again for each page under
pagination. I need to be able to label records 1 - 100 across several
pages.
Thanks

Alex Koshelev

unread,
Mar 17, 2009, 12:51:52 PM3/17/09
to django...@googlegroups.com
You can use `forloop.counter` with `sum` filter. Or use `ol` html tag
with proper `start` attribute.

Jesse

unread,
Mar 17, 2009, 1:46:03 PM3/17/09
to Django users
I'm please to know of such a tag. Do you know of a good example of
how it is used with the sum filter? Otherwise, I will try.

Thanks!

On Mar 17, 9:51 am, Alex Koshelev <daeva...@gmail.com> wrote:
> You can use `forloop.counter` with `sum` filter. Or use `ol` html tag
> with proper `start` attribute.
>

Jacob Kaplan-Moss

unread,
Mar 17, 2009, 1:48:27 PM3/17/09
to django...@googlegroups.com
On Tue, Mar 17, 2009 at 12:46 PM, Jesse <adle...@gmail.com> wrote:
> I'm please to know of such a tag.  Do you know of a good example of
> how it is used with the sum filter?  Otherwise, I will try.

I don't know why you'd need the sum filter; the for tag does
everything you'd want. See
http://docs.djangoproject.com/en/1.0/ref/templates/builtins/#for for
details of the {% for %} tag. You want something like::

{% for record in record_list %}
{{ forloop.counter }}: {{ record }}
{% endfor %}

Jacob

Alex Gaynor

unread,
Mar 17, 2009, 1:49:08 PM3/17/09
to django...@googlegroups.com
Jacob, if I understand the issue is that on the 2nd page he wants to start the count at 101, not 1.

Alex

--
"I disapprove of what you say, but I will defend to the death your right to say it." --Voltaire
"The people's good is the highest law."--Cicero
Reply all
Reply to author
Forward
0 new messages