遍历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))
模板
{% 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
我试过了
{% for company in category.company_set.all() %}
似乎 django 不喜欢模板中的 ()
seems django doesn't like () in templates
django 站点上有迷宫般的信息,我一直在 .96、1.0 和开发版本之间迷失.我正在运行 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屋!
查看全文