我想将之前的帖子和下一个帖子功能添加到博客页面,它运行正常,但是当我向博客添加主动被动功能时它不起作用。
这是我的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]
如何解决这个问题?
答案 0 :(得分:0)
看一下本教程。 https://www.djangorocks.com/tutorials/how-to-create-a-basic-blog-in-django/defining-your-models.html
答案 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()