Wednesday, June 26, 2019

Getting posts of a category in blog application

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



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/fdf49d34-3aea-41e6-b4e0-0d810a62d222%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment