访问多对多值Django

时间:2011-11-02 11:11:46

标签: django django-models django-views

article = get_object_or_404(Article,slug=slug)
categories = article.category.all()

使用render_to_response(),如何在视图中使用类别?

1 个答案:

答案 0 :(得分:1)

假设您在模板中有该文章,您可以执行以下操作:

# In your view
return render_to_response('page.html', {'article': article})

# In your template
{% for category in article.category.all %}
    {{ category.attribute }}
{% endfor %}

# Or, if you already have the categories
return render_to_response('page.html', {'categories': categories})
{% for category in categories %}
    {{ category.attribute }}
{% endfor %}