Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django 'Manager' object is not iterable (but the code works in the manage.py shell)

I'm new to django and created an application not much different from the polls website described in the tutorial. In the website I get:

Exception Type: TemplateSyntaxError
Exception Value:    
Caught TypeError while rendering: 'Manager' object is not iterable
Exception Location: /usr/lib/python2.7/dist-packages/django/template/defaulttags.py in render, line 190

pointing to the template marking the error lin line 4 (Caught TypeError while rendering: 'Manager' object is not iterable):

test
2   {% if clips %}
3       <ul>
4       {% for aclip in clips %}
5           <li><a href="/annotate/{{ aclip.id }}/">{{ aclip.name }}</a></li>
6       {% endfor %}
7       </ul>
8   {% else %}
9       <p>No clips are available.</p>
10  {% endif %}

Here is the clip object:

class Clip(models.Model):
    def __unicode__(self):
        return self.name
    name = models.CharField(max_length=30)
    url = models.CharField(max_length=200)

And the view code:

def index(request):
    #return HttpResponse("You're looking at clips.")
    mylist = []
    latest_clip_list = Clip.objects.all()#.values_list("name","id")#.order_by('-pub_date')[:5]
    print latest_clip_list
    return render_to_response('annotateVideos/index2.html', {'clips': latest_clip_list})

When I run this code from the manage,py shell there is no exception:

In [2]: from annotateVideos import views

In [3]: f = views.index("")
[{'id': 6L, 'name': u'segment 6000'}]

In [4]: f.content
Out[4]: 'test\n\n    <ul>\n    \n        <li><a href="/annotate//"></a></li>\n    \n        </ul>\n'

Any ideas? I find it hard to debug as the code seems to be working on the shell but not on the web server.

Thanks, Raphael

like image 517
user2058328 Avatar asked Feb 10 '13 07:02

user2058328


1 Answers

You have a lot of parts commented out in your view code, specifically in the line

latest_clip_list = Clip.objects.all()#.values_list("name","id")#.order_by('-pub_date')[:5]

The error you are getting, 'Manager' object is not iterable, would indicate that the for loop in your template is trying to iterate over the manager Clip.objects, and not the queryset Clip.objects.all().

Double-check to make sure that your view actually reads

latest_clip_list = Clip.objects.all()

and doesn't just look like

latest_clip_list = Clip.objects
like image 187
Ian Clelland Avatar answered Oct 16 '22 10:10

Ian Clelland