根据用户类型区分对帖子的访问

我正在开发一个博客,我需要根据用户类型对帖子进行区分访问。如果帖子是草稿,则只有员工用户可以阅读,否则每个人都可以阅读。


我将此代码放入views.py:


geopost_filter = GeoPost.objects.filter(Q(draft=False) and Q(publishing_date__lte=timezone.now()))


def single_geopost(request, slug_post):

    if request.user.is_staff:

        geopost = get_object_or_404(GeoPost, slug_post=slug_post)

    else:

        geopost = get_object_or_404(geopost_filter, slug_post=slug_post)

    context = {

        "post": geopost,

        }

    template = 'geoblog/single_geopost.html'

    return render(request, template, context)

此功能无法运行,因为无论我是工作人员还是普通用户,我都可以阅读帖子。我哪里错了?


隔江千里
浏览 85回答 1
1回答

慕码人2483693

# Just need to change your geopost_filter querygeopost_filter = GeoPost.objects.filter(draft=False, publishing_date__lte=timezone.now())def single_geopost(request, slug_post):    if request.user.is_staff:        geopost = get_object_or_404(GeoPost, slug_post=slug_post)    else:        geopost = get_object_or_404(geopost_filter, slug_post=slug_post)    context = {        "post": geopost,        }    template = 'geoblog/single_geopost.html'    return render(request, template, context)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python