DJango, the preivous post or the next post according to that category list
I am studying to develope a blog website using DJango's generic view class.
I categorized blogs into categories.
If you select a blog from the full list and go to the detail page, you have implemented the ability to go to the previous post and the next post using "previous()" and "next()" in that post.
I want to select a blog from a category list and move it to the preivous post or the next post according to that category list, but I don't know how.
What can I do?
models.py
class Post(models.Model):
..........
def prev(self):
return self.get_previous_by_created_at()
def next(self):
return self.get_next_by_created_at()
views.py
class PostListByCategory(PostList): def get_queryset(self): slug = self.kwargs['slug'] if slug == 'no_category': category = None else: category = Category.objects.get(slug=slug) return Post.objects.filter(category=category).order_by('-pk')
def get_context_data(self, **kwargs):
context = super(PostListByCategory, self).get_context_data()
slug = self.kwargs['slug']
if slug == 'no_category':
category = 'None'
else:
category = Category.objects.get(slug=slug)
context['category'] = category
return context
post_detail.html
..............
<div class="row row-cols-auto">
{% if object.next %}
<div class="col">
<a href="{% url 'tube_detail' object.next.pk %}" class="btn btn-sm pt-1 pb-1 bg-light" title="Prev">
<i class="bi bi-caret-left-fill" style="color:#dd3535"></i>
</a>
</div>
{% endif %}
<div class="col">
<a href="{% url 'tube_list' %}{% if request.GET.category %}category/{{ request.GET.category}}{% endif %}" class="btn btn-sm pt-1 pb-1 bg-light" title="LIST">
<i class="bi bi-list" style="color:#dd3535"></i>
</a>
</div>
{% if object.prev %}
<div class="col">
<a href="{% url 'tube_detail' object.prev.pk %}" class="btn btn-sm pt-1 pb-1 bg-light" title="Next">
<i class="bi bi-caret-right-fill" style="color:#dd3535"></i>
</a>
</div>
{% endif %}
</div>
''''''''''''