简体   繁体   中英

Traversing foreign key related tables in django templates

View

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 tried

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

seems django doesn't like () in templates

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

Just get rid of the parentheses:

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

Here's the appropriate documentation . You can call methods that take 0 parameters this way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM