How can I show newest posts and comments on the top in my blogging website?
Question:
I have successfully made the models for both comments and posts and they are showing up properly on the webpage but I want the newest post to be showed up first and the same for comments as well.
views.py:
from django.shortcuts import render, HttpResponse, redirect
from blog.models import Post, BlogComment
from django.contrib import messages
# Create your views here.
def blogHome(request):
allpost = Post.objects.all()
context = {'allposts': allpost}
return render(request, 'blog/blogHome.html', context)
def blogPost(request, slug):
post = Post.objects.filter(slug=slug).first()
comments = BlogComment.objects.filter(post=post)
context = {'post': post, 'comments': comments}
return render(request, 'blog/blogPost.html', context)
def postComment(request):
if request.method == 'POST':
comment = request.POST.get('comment')
user = request.user
postSno = request.POST.get("postSno")
post = Post.objects.get(sno=postSno)
comment = BlogComment(comment=comment, user=user, post=post)
comment.save()
messages.success(request, 'your comment has been added')
return redirect(f"/blog/{post.slug}")
this is the blog home page where I want the newest post to be displayed first
blogHome.html:
{% extends 'base.html' %}
{% block title %} blogs {% endblock title %}
{% block blogactive %}active {% endblock blogactive %}
{% block body %}
<h2 class="text-center my-4">blogs by everythingcs</h2>
<div class="container">
{% for post in allposts %}
<div class="col-md-6">
<div class="row no-gutters border rounded overflow-hidden flex-md-row mb-4 shadow-sm h-md-250 position-relative">
<div class="col p-4 d-flex flex-column position-static">
<strong class="d-inline-block mb-2 text-primary">by-{{post.author}}</strong>
<h3 class="mb-0">{{post.title}}</h3>
<div class="mb-1 text-muted">Nov 12</div>
<p class="card-text mb-auto">{{post.content|truncatechars:200}}</p>
<div class="my-2">
<a href="/blog/{{post.slug}}" role="button" class="btn btn-primary">More..</a>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock body %}
finally the models.py for further reference:
from django.db import models
from django.contrib.auth.models import User
from django.utils.timezone import now
# Create your models here.
class Post(models.Model):
sno = models.AutoField(primary_key=True)
title = models.CharField(max_length=50)
content = models.TextField()
author = models.CharField(max_length=50)
slug = models.SlugField(max_length=200)
timeStamp = models.DateTimeField(blank=True)
def __str__(self):
return self.title + " by " + self.author
class BlogComment(models.Model):
sno = models.AutoField(primary_key=True)
comment = models.TextField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True)
timestamp = models.DateTimeField(default=now)
def __str__(self):
return self.comment[0:13] + "..." + "by " + self.user.username
in short, I want to sort my blog post and blog comments by their time and then display them accordingly.
Answers:
You can specify a default ordering
[Django-doc] in the Meta
[Django-doc] of the objects:
class Post(models.Model):
sno = models.AutoField(primary_key=True)
title = models.CharField(max_length=50)
content = models.TextField()
author = models.CharField(max_length=50)
slug = models.SlugField(max_length=200)
timestamp = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['-timestamp']
def __str__(self):
return f'{self.title} by {self.author}'
class BlogComment(models.Model):
sno = models.AutoField(primary_key=True)
comment = models.TextField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True)
timestamp = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['-timestamp']
def __str__(self):
return f'{self.comment[0:13]}... by {self.user.username}'
or you can order explicitly:
def blogHome(request):
allpost = Post.objects.order_by('-timestamp')
context = {'allposts': allpost}
return render(request, 'blog/blogHome.html', context)
Note: normally the name of the fields in a Django model are written in snake_case, not PerlCase, so it should be: timestamp
instead of timeStamp
.
Note: It is normally better to make use of the settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use the User
model [Django-doc] directly. For more information you can see the referencing the User
model section of the documentation.
Note: Django’s DateTimeField
[Django-doc]
has a auto_now_add=…
parameter [Django-doc]
to work with timestamps. This will automatically assign the current datetime
when creating the object, and mark it as non-editable (editable=False
), such
that it does not appear in ModelForm
s by default.
I have successfully made the models for both comments and posts and they are showing up properly on the webpage but I want the newest post to be showed up first and the same for comments as well.
views.py:
from django.shortcuts import render, HttpResponse, redirect
from blog.models import Post, BlogComment
from django.contrib import messages
# Create your views here.
def blogHome(request):
allpost = Post.objects.all()
context = {'allposts': allpost}
return render(request, 'blog/blogHome.html', context)
def blogPost(request, slug):
post = Post.objects.filter(slug=slug).first()
comments = BlogComment.objects.filter(post=post)
context = {'post': post, 'comments': comments}
return render(request, 'blog/blogPost.html', context)
def postComment(request):
if request.method == 'POST':
comment = request.POST.get('comment')
user = request.user
postSno = request.POST.get("postSno")
post = Post.objects.get(sno=postSno)
comment = BlogComment(comment=comment, user=user, post=post)
comment.save()
messages.success(request, 'your comment has been added')
return redirect(f"/blog/{post.slug}")
this is the blog home page where I want the newest post to be displayed first
blogHome.html:
{% extends 'base.html' %}
{% block title %} blogs {% endblock title %}
{% block blogactive %}active {% endblock blogactive %}
{% block body %}
<h2 class="text-center my-4">blogs by everythingcs</h2>
<div class="container">
{% for post in allposts %}
<div class="col-md-6">
<div class="row no-gutters border rounded overflow-hidden flex-md-row mb-4 shadow-sm h-md-250 position-relative">
<div class="col p-4 d-flex flex-column position-static">
<strong class="d-inline-block mb-2 text-primary">by-{{post.author}}</strong>
<h3 class="mb-0">{{post.title}}</h3>
<div class="mb-1 text-muted">Nov 12</div>
<p class="card-text mb-auto">{{post.content|truncatechars:200}}</p>
<div class="my-2">
<a href="/blog/{{post.slug}}" role="button" class="btn btn-primary">More..</a>
</div>
</div>
</div>
</div>
{% endfor %}
</div>
{% endblock body %}
finally the models.py for further reference:
from django.db import models
from django.contrib.auth.models import User
from django.utils.timezone import now
# Create your models here.
class Post(models.Model):
sno = models.AutoField(primary_key=True)
title = models.CharField(max_length=50)
content = models.TextField()
author = models.CharField(max_length=50)
slug = models.SlugField(max_length=200)
timeStamp = models.DateTimeField(blank=True)
def __str__(self):
return self.title + " by " + self.author
class BlogComment(models.Model):
sno = models.AutoField(primary_key=True)
comment = models.TextField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True)
timestamp = models.DateTimeField(default=now)
def __str__(self):
return self.comment[0:13] + "..." + "by " + self.user.username
in short, I want to sort my blog post and blog comments by their time and then display them accordingly.
You can specify a default ordering
[Django-doc] in the Meta
[Django-doc] of the objects:
class Post(models.Model):
sno = models.AutoField(primary_key=True)
title = models.CharField(max_length=50)
content = models.TextField()
author = models.CharField(max_length=50)
slug = models.SlugField(max_length=200)
timestamp = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['-timestamp']
def __str__(self):
return f'{self.title} by {self.author}'
class BlogComment(models.Model):
sno = models.AutoField(primary_key=True)
comment = models.TextField()
user = models.ForeignKey(User, on_delete=models.CASCADE)
post = models.ForeignKey(Post, on_delete=models.CASCADE)
parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True)
timestamp = models.DateTimeField(auto_now_add=True)
class Meta:
ordering = ['-timestamp']
def __str__(self):
return f'{self.comment[0:13]}... by {self.user.username}'
or you can order explicitly:
def blogHome(request):
allpost = Post.objects.order_by('-timestamp')
context = {'allposts': allpost}
return render(request, 'blog/blogHome.html', context)
Note: normally the name of the fields in a Django model are written in snake_case, not PerlCase, so it should be:
timestamp
instead of.timeStamp
Note: It is normally better to make use of the
settings.AUTH_USER_MODEL
[Django-doc] to refer to the user model, than to use theUser
model [Django-doc] directly. For more information you can see the referencing theUser
model section of the documentation.
Note: Django’s
DateTimeField
[Django-doc]
has aauto_now_add=…
parameter [Django-doc]
to work with timestamps. This will automatically assign the current datetime
when creating the object, and mark it as non-editable (editable=False
), such
that it does not appear inModelForm
s by default.