In my blog details page I want to add next and previous button for only related blog post. Assume I write three blog on python and two on java. If user on java tutorial-1 details page then I want to show next button for move on java tutoral-2 page and also want previous button to go back java-tutorial-1 page.
#most important note I don’t want to show next button if he is on java-tutorial-2 page as I have only two blog post on java. So I want to show previous and next button only for related blog post not for my all blog post. see the layout below for better understand.
layout:
-------Blog Title----------parent object---------------button---------------------------
- python tutorial-1------->No --------------------->want only next button |
- python tutorial-2------->python tutorial-1------->want next and previous button |
- python tutorial-3------->python tutorial-2-------->want only previous button |
- java tutorial-1--------->No ---------------------->want only next button |
- java tutorial-2---------->java tutorial-1--------->want only previous button |
----------------------------------------------------------------------------------------
here is my code:
models.py
class Blog(models.Model):
blog_title = models.CharField(max_length=255)
blog_parent = models.ForeignKey(
'self', on_delete=models.CASCADE, blank=True, null=True)
views.py
def blogDetails(request, slug=None):
blog = Blog.objects.filter(blog_slug=slug)
context = {"blog": blog}
return render(request, 'blog/blogDetails.html', context)