仅当用户是员工时才使 post 可读

Make readable the post only if the user is staff

我正在开发博客的后端,我需要区分 posts 的类型:publishedfuture post草稿。为此,我从我过去的请求中的指示开始:

在我意识到 post 的所有类型总是在线之后,谢谢 我只能将已发布的 post 放在网上。

在我的博客中有两种类型的用户:is_staff(默认的Django类型),is_user(它在我的注册模型中)。还有另一种类型是匿名用户,即使用 Google 或其他解决方案到达我博客的没有任何类型注册的用户。

因此我开发了一个视图,仅当用户是 is_staff 时才显示草稿和未来的 post,但我看到 禁止错误.

def singlePost(request, slug_post, slug_category):
    post_category = get_object_or_404(BlogCategory, slug_category=slug_category)
    post_filter = BlogPost.objects.filter(draft=False, publishing_date__lt=datetime.datetime.now())
    if not request.user.is_authenticated:
         post_filter = BlogPost.objects.filter(draft=False, publishing_date__lt=datetime.datetime.now())
         raise PermissionDenied
    elif request.user.is_user:
         post_filter = BlogPost.objects.filter(draft=False, publishing_date__lt=datetime.datetime.now())
         raise PermissionDenied
    else:
         post_filter = BlogPost.objects.all()
    post_details = get_object_or_404(post_filter, slug_post=slug_post)
    category_post_details = BlogPost.objects.filter(post_category=post_category)
    context = {
        "post_category": post_category,
        "post_details": post_details,
        "category_post_details": category_post_details,
        }
    template = 'blog/reading/single_post.html'
    return render(request, template, context)

我该如何解决? 自己创建个人博客是一个了解更多 Python 和 Django 的机会。

注意:视图以这种方式工作正常

def singlePost(request, slug_post, slug_category):
    post_category = get_object_or_404(BlogCategory, slug_category=slug_category)
    post_details = get_object_or_404(BlogPost, slug_post=slug_post)
    category_post_details = BlogPost.objects.filter(post_category=post_category)
    context = {
        "post_category": post_category,
        "post_details": post_details,
        "category_post_details": category_post_details,
        }
    template = 'blog/reading/single_post.html'
    return render(request, template, context)

NB: That I would like obtain is a backend like Wordpress. With Wordpress you can create a draft or a scheduled post, this type of posts are not online and make readable only to the loggedin users.

感谢@RaideR 的指点,解决了我的问题

def singlePost(request, slug_post, slug_category):
    post_category = get_object_or_404(BlogCategory, slug_category=slug_category)
    if not request.user.is_staff:
         post_filter = BlogPost.objects.filter(
                        draft=False,
                        publishing_date__lt=datetime.datetime.now()
                        )
    else:
         post_filter = BlogPost.objects.all()
    post_details = get_object_or_404(post_filter, slug_post=slug_post)
    category_post_details = BlogPost.objects.filter(post_category=post_category)
    context = {
        "post_category": post_category,
        "post_details": post_details,
        "category_post_details": category_post_details,
        }
    template = 'blog/reading/single_post.html'
    return render(request, template, context)