R
R
Rem362018-04-16 08:41:42
Django
Rem36, 2018-04-16 08:41:42

How to make categories?

Good afternoon. I can't figure out how to make categories. Skinte please an example. I found a vidos with categories, I tried it, but something was not right. There was such

{% for c in categories %}
{{ c.name }}
{for p in posts}
{{p.name}}
{% endfor %}

In views it takes categories = Category.objects.all() and posts = Post.objects.all()
So did but it displays everything in all categories. How to make that would display under each of its own.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Tikhonov, 2018-04-16
@tumbler

1) the Post must have a ForeignKey on the category
2) Then instead of posts in the template it will be possible to use c.post_set.all
3) It turns out that for each category there is a cycle through all the posts whose ForeignKey value corresponds to this category

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question