Closed alvindera97 closed 5 years ago
Pretty hard to diagnose without a reproducible example, but you need to make your ordering field non-nullable or results can be inconsistent (see docs: https://github.com/gregplaysguitar/django-next-prev#ordering-considerations)
Perfect! Thank you so much. For future reference purposes, all that needed to be done was to change the ordering field from null=True
to null=False
. So my model for the blog posts had a little change at the end:
blog_post_author = models.ForeignKey(Author,
null=True,
on_delete=models.CASCADE)
blog_post_category = models.ForeignKey(BlogCategory,
null=True,
on_delete=models.CASCADE)
blogpost_title = models.CharField("Title",
max_length=200,
help_text="If necessary, make minimal use of ?s, !s, \\s and :s")
blog_post_publish_date = models.DateField("Publish Date", blank=True, null=False)
I'm working on a part time project that has a blog section. A section of the blog deals with displaying the next (published) blog post and the previously published blog post - all with respect to the blog post currently viewed.
THIS MEANS THAT IF A CLIENT IS VIEWING POST 3, PREVIOUS_POST == POST 1, NEXT_POST == POST 4
This is the model.py section of the portion pertaining to the BlogPost model.
Using the django framework, this is the view section for computing the previous blog post:
all_blog_posts = get_object_or_404(BlogPost, id=id)
all_blog_posts just returns a BlogPost object. The blog post currently viewed.
next blog post:
The next_post() function works properly, returning the next blog post as designed to. However, the previous_post() function fails and instead returns the first blog post all the time. Even trying the 'prev_in_order()' function separately, displayed the same results i.e. returning only the very first blog post all the time.
This might not (in eventuality) count as an issue but I cannot after several glances find out what is wrong with my code. Thank you. I hope a favorable reply. Cheers.