在django模板中遍历外键相关表 [英] Traversing foreign key related tables in django templates
本文介绍了在django模板中遍历外键相关表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
categories = Category.objects.all()
t = loader.get_template('index.html')
v = Context({
'categories': categories
})
return HttpResponse(t.render(v))
模板
Template
{% for category in categories %}
<h1>{{ category.name }}</h1>
{% endfor %}
这个功能很好。现在我试图打印该类别中的每家公司。公司表有一个外键到类别表
this works great. now im trying to print each company in that category. the company table has a foreign key to the category table
ive尝试
{% for company in category.company_set.all() %}
似乎django不like()in templates
seems django doesn't like () in templates
在django网站上有一个迷宫的信息,我一直在.96,1.0和dev版本之间迷失。 im运行django版本1.0.2
There's a maze of information on the django site i keep getting lost between the .96, 1.0 and dev version. im running django version 1.0.2
推荐答案
只需删除括号:
{% for company in category.company_set.all %}
以下是适当的文档。您可以调用以这种方式取0参数的方法。
Here's the appropriate documentation. You can call methods that take 0 parameters this way.
这篇关于在django模板中遍历外键相关表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文