Hey Mike,
Could you tell us what it is that you're expecting from the code? There's a
couple of things in it that I couldn't understand:

   1. Why are you raising a NameError exception for every element in get?
   Is that supposed to be debugging to find out if the loop is running?
   2. What are the next and previous variables for? They're not used
   anywhere in the snippet

That said, I'm not sure what you're trying to get but there's some points
to consider

   1. Is item_id an integer? If it isn't that may be the reason get is
   coming up empty.
   2. list is a reserved keyword in python, so you should avoid using it as
   a variable name.
   3. If you want a list of all the element id's from the database, you're
   better off with Item.objects.values_list('id', flat=True)
   4. To get the position of an element inside a list, use position =
   list.index(item_id)

I think that's all I have. Hope it helps,

Gabe


On Fri, May 3, 2013 at 10:32 AM, MikeKJ <mike.jo...@paston.co.uk> wrote:

> I am using enumerate to find the position in a list but it doesnt seem to
> be working or throwing an error any ideas please?
>
> def detail(request, item_id):
>     item = Item.objects.get(pk=item_id)
>     list = []
>     count = Item.objects.all().count()
>     all = Item.objects.all()
>     for y in all:
>         list.append(y.id)
>     get = (i for i,x in enumerate(list) if x==item_id)
>     for i in get:
>         raise NameError(i)
>         if i > 0:
>             previous = list[i - 1]
>         if i < (count-1):
>             next = list[i + 1]
>     return render_to_response('news/item_detail.html',{ 'item': item,},
> context_instance=RequestContext(request))
>
> cheers
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to django-users+unsubscr...@googlegroups.com.
> To post to this group, send email to django-users@googlegroups.com.
> Visit this group at http://groups.google.com/group/django-users?hl=en.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to