获取django下一篇文章和上一篇文章?

时间:2017-06-19 08:54:04

标签: python django

我想将之前的帖子和下一个帖子功能添加到博客页面,它运行正常,但是当我向博客添加主动被动功能时它不起作用。

这是我的view.py

    model = Blog
queryset = Blog.objects.filter(is_active__exact=True)
template_name = 'site/Blog/post.html'
pk_url_kwarg = "id"
slug_url_kwarg = 'slug'
slug_field = 'slug_tr'
query_pk_and_slug = True

def get_context_data(self, **kwargs):
    context = super(BlogDetailView, self).get_context_data(**kwargs)
    recent_posts = Blog.objects.all().order_by('-created_at')[:3]
    comments = BlogComment.objects.filter(target_blog__exact=self.object)
    previous_post = Blog.objects.filter(is_active=True).filter(created_at__lt=self.object.created_at).order_by('-created_at')[:1]

    next_post_c = Blog.objects.filter(is_active=True).filter(created_at__gt=self.object.created_at).order_by('-created_at')[:1]

如何解决这个问题?

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

问题已解决

   previous_post = Blog.objects.filter(is_active=True, created_at__lt=self.object.created_at).order_by('-created_at').first()

   next_post_c = Blog.objects.filter(is_active=True, created_at__gt=self.object.created_at).order_by('-created_at').first()