django在模板中显示m2m元素

时间:2010-06-17 20:26:20

标签: django templates m2m

如果有一个像

这样的声明
 def inside_classroom(request,classname):


    theclass = Classroom.objects.get(classname = classname)
    members = theclass.members.all()
c = Courses.objects.filter(classroom = theclass)


return render_to_response('classroom/inside_classroom.html', {
    'theclass': theclass,
    'c':c,
    'members':members, 

    }, 
    context_instance=RequestContext(request)) 

我希望在模板中显示所有成员(类),我该怎么办?

如果我写:

{{theclass.members.all}}

输出是一个空列表(尽管该类有一些成员)

如何在模板中显示m2m表的元素? 谢谢!

1 个答案:

答案 0 :(得分:0)

您应该将members放在上下文中,然后在模板中迭代所有成员,例如。

{% for member in members %}
   {{ member.name }}<br />
   {{ member.xxxx }}
{% endfor %}