Thanks for the hints Tim. The problem is that i'm not using the whole
sequence, im just pulling one item out of the query set and was hoping for a
way to get the position without having to use the whole set, in the same
vain that count() is much more sensible a choice than len() as it gets
optimised down to an SQL count by django. Thanks for pointing out enumerate
though, im actually embarrassed i didn't know that, it looks very very
useful.

Tom

On Jan 3, 2008 3:12 PM, Tim Chase <[EMAIL PROTECTED]> wrote:

>
> > In my template, i want to be able to print "Item X of Y" where
> > items are got from a QuerySet such as
> > Picture.objects.filter(album='name')
> >
> > Now Y is easy to get, its just the count() of the queryset,
> > however i cant seem to find a solution to find X. The objects
> > do have an ordering set, so they should be in the same order
> > each time the query is run. I know its bad, but i tried to
> > just to a pictures.index(picture) just for testing, but
> > QuerySet doesnt provide that method (sensibly). Any pointers?
>
> It sounds like instead of passing your queryset directly to the
> view/template, you might want to pass "enumerate(qs)"[1] to your
> view/template.  This returns pairs of zero-based indicies that
> you can use for your X.  Use the "add" filter[2] to make them
> 1-based indices.
>
> Or more easily, if you're iterating over the list of items in
> your view, you can use the "forloop.counter" property[3]:
>
>   <dl>
>   {% for picture in pictures %}
>     <dt>Item {{ forloop.counter }} of {{ pictures.count }}</dt>
>     <dd>{{ picture }}</dd>
>   {% endfor %}
>   </dl>
>
> -tim
>
> [1] http://docs.python.org/lib/built-in-funcs.html#l2h-24
> [2] http://www.djangoproject.com/documentation/templates/#add
> [3] http://www.djangoproject.com/documentation/templates/#for
>
> >
>


-- 
Tom Badran
http://badrunner.net

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to