How can display latest 3 posts category wise?

def index(request):
    category_list = Category.objects.all()
    latest_post = Post.objects.all().order_by('-post_date')[:3]
    context = {
        'category_list': category_list,
    return render(request, 'Software/index.html', context)
  {% for  categor in  category_list %}
        <h3>{{ categor.category_name }}</h3>
        {% for post in categor.post_set.all %}
            <li>{{ post.title }}</li>
    {% endfor %}
    {% endfor %}
from django.db import models
from django.urls import reverse
from django.utils.text import slugify

# Create your models here.

class Category(models.Model):
    category_name = models.CharField(max_length=50, unique=True, null=True)
    category_slug = models.SlugField(unique=True, max_length=150, blank=True)

class Post(models.Model):
    title = models.CharField(max_length=50)
    post_date = models.DateTimeField()
    category = models.ForeignKey(Category, max_length=50, on_delete=models.CASCADE, null=True, blank=True)

I want output like
Category 1
Category 2

Use Post model to get the category name like

qs = Post.objects.filter(category__category_name="the_category_name_or_an_instance_of_the_category_model").order_by(‘-post_date’)

Because the “Post” model has a relation with the “Category” model so you can access its fields.
Note the “double underscore” here category__category_name

1 Like

Side note: When posting code, templates, error messages, etc here, enclose it between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code (templates, etc), then another line of ```. This forces the forum software to keep your code properly formatted.


Thank You .
But Not Working.

You may miss something or you may copy my query without making a slice.
Also feed us by helping INFO.
In order to enable to help.

1 Like

You’re directly referencing the related object manager “all” here - this is going to issue a query to retrieve all related Post. (Most importantly, it’s not going to use the other query you’ve defined on Post.)

There are a couple different ways to address this. It’s up to you to decide which approach works best for you.

You can:

  • Create a model method in Category to retrieve the latest three Post, and iterate over it in your template.

  • If you’re using PostgreSQL, you could use the ArrayAgg function to annotate the list of the latest three Post in Category, and iterate over that list

  • Perform your query on Post and reorganize the data within your view before sending it to the template.

I’m sure there are others.


Sir, Can you me?

  • Create a model method in Category to retrieve the latest three Post, and iterate over it in your template
    How to implement code?

Review the docs at Variables, it defines how you can reference a method on a model in a template.

If necessary, also review the docs at:

Give it a try, see what you come up with, and feel free to ask further questions if you need more help.

1 Like

Thank you for your precious time.