[Django]-How do i show recent posts first in Django blog?

8👍

✅

from django.db import models

class Blog(models.Model):
    title = models.CharField(max_length=32)
    date = models.DateTimeField(auto_now_add=True)
    text = models.TextField()

    class Meta:
        ordering = ['-date',]

https://docs.djangoproject.com/en/dev/topics/db/models/#meta-options

or do it with when you create the queryset

Blog.objects.all().order_by('-date')

https://docs.djangoproject.com/en/dev/ref/models/querysets/#order-by

1👍

  • This is how to work for me, not a new solution, only more detailed.
  • in my views.py
def blog(request):
    post_list = Post.objects.all().order_by('-timestamp')


    context = {
        'post_list': post_list,
    }
    return render(request, 'blog/blog.html', context)

  • Thank you.

Leave a comment