How to get categories for a post

17 views
Skip to first unread message

garymc

unread,
Jun 26, 2008, 9:11:42 PM6/26/08
to Django Basic Apps
I'm trying to get a list of categories for posts in the post_detail
and post_list templates. I want to get the specific categories that
each post belongs to.

I tried adapting the code used to get a list of tags but that doesn't
work.

<ul>
{% categories_for_object as category_list %}
{% for category in category_list %}
<li><a href="#15">{{ category }}</a> //</li>
{% endfor %}
</ul>

I'm sure there's an easy way to do this but I can't figure it out.

Thanks,
garymc

John Szakmeister

unread,
Jun 26, 2008, 9:57:46 PM6/26/08
to django-b...@googlegroups.com

I think you want:
{% for category in post.categories %}


<li><a href="#15">{{ category }}</a> //</li>
{% endfor %}

I haven't actually tried it (haven't had a need)... but I think that'll do it.

-John

garymc

unread,
Jun 26, 2008, 10:42:17 PM6/26/08
to Django Basic Apps
Thanks John,
Unfortunately that doesn't seem to work. I get a blank in the
post_detail page and a error on the post_list page
('ManyRelatedManager' object is not iterable).

On Jun 27, 2:57 am, "John Szakmeister" <j...@szakmeister.net> wrote:

garymc

unread,
Jun 26, 2008, 10:48:52 PM6/26/08
to Django Basic Apps
Figured it out.

{% for category in post.categories.all %}
<li><a href="#15">{{ category }}</a> //</li>
{% endfor %}

I had to add ".all" to post.categories in the post_list template and
change post.categories.all to object.categories.all on the post_detail
template.

Thanks again.

John Szakmeister

unread,
Jun 27, 2008, 5:07:36 AM6/27/08
to django-b...@googlegroups.com
On Thu, Jun 26, 2008 at 10:48 PM, garymc <mccaf...@gmail.com> wrote:
>
> Figured it out.
>
> {% for category in post.categories.all %}
> <li><a href="#15">{{ category }}</a> //</li>
> {% endfor %}
>
> I had to add ".all" to post.categories in the post_list template and
> change post.categories.all to object.categories.all on the post_detail
> template.
>
> Thanks again.

Ah, that's right. Glad you got it working!

-John

Reply all
Reply to author
Forward
0 new messages