Getting posts of a category in blog application

5 views
Skip to first unread message

Gaurav Sahu

unread,
Jun 26, 2019, 1:06:19 PM6/26/19
to Django users
I want to grab all the posts of a particular category. I am passing th url like this: {% url 'blog:post_list' %}?category={{ cat }}. But I am not able how to return the queryset for posts of the category cat passed in the url.

my Post model:

class Post(models.Model):
    title
= models.CharField(max_length=100, unique = True)
    overview
= RichTextUploadingField()
    created_at
= models.DateTimeField(editable=False)
    published_at
= models.DateTimeField( null=True)
    updated_at
= models.DateTimeField()
    content
= RichTextUploadingField(blank=False, null=True)
   
# comment_count = models.IntegerField(default = 0)
   
# view_count = models.IntegerField(default = 0)
    author
= models.ForeignKey(User, on_delete=models.CASCADE, related_name='posts')
    thumbnail
= ImageField(blank=True, null=True)
    categories
= models.ManyToManyField(Category, related_name='posts')
    featured
= models.BooleanField(blank=True, default=False)
    slug
= models.SlugField(max_length = 250, null = True, blank = True, unique = True)


            <h1 class="mb-4">{{post.title|safe}}</h1>
            {% for cat in post.categories.all %}
            <a class="category mb-5" href="{% url 'blog:post_list' %}?category={{ cat }}">{{cat}}</a>
            {% endfor %}



Reply all
Reply to author
Forward
0 new messages