【问题标题】:Traversing foreign key related tables in django templates遍历django模板中的外键相关表
【发布时间】:2010-11-04 02:47:02
【问题描述】:

查看

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 %}

这很好用。现在我正在尝试打印该类别中的每家公司。 company 表有一个到 category 表的外键

试过了

{% for company in category.company_set.all() %}

似乎 django 不喜欢模板中的 ()

在 .96、1.0 和开发版本之间,我不断迷失在 django 网站上的信息迷宫。我正在运行 django 版本 1.0.2

【问题讨论】:

    标签: python django django-models django-templates


    【解决方案1】:

    去掉括号:

    {% for company in category.company_set.all %}
    

    这是appropriate documentation。您可以通过这种方式调用采用 0 参数的方法。

    【讨论】:

    猜你喜欢
    • 2019-11-24
    • 1970-01-01
    • 2018-02-24
    • 2010-10-21
    • 2020-04-04
    • 2011-01-25
    • 2011-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多