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

I am new to django and created an application that is not much different from the survey site described in the tutorial. On 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 a template indicating a line of error line 4 (Caught TypeError when rendering: the 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 a control shell, py 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? It's hard for me to debug, as the code seems to work with the shell, but not on the web server.

Thanks Raphael

+5
source share
1 answer

There are many parts to your view code, especially the line

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

, , 'Manager' object is not iterable, , for Clip.objects, Clip.objects.all().

, ,

latest_clip_list = Clip.objects.all()

latest_clip_list = Clip.objects
+10

All Articles