Доступ к ценностям «многие ко многим» Django - PullRequest
0 голосов
/ 02 ноября 2011
article = get_object_or_404(Article,slug=slug)
categories = article.category.all()

Используя render_to_response(), как я могу использовать категории в представлении?

1 Ответ

1 голос
/ 02 ноября 2011

Если у вас есть статья в шаблоне, вы можете сделать следующее:

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