我正在开发一个博客,我需要根据用户类型对帖子进行区分访问。如果帖子是草稿,则只有员工用户可以阅读,否则每个人都可以阅读。我将此代码放入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)此功能无法运行,因为无论我是工作人员还是普通用户,我都可以阅读帖子。我哪里错了?
1 回答
慕码人2483693
TA贡献1860条经验 获得超9个赞
# Just need to change your geopost_filter query
geopost_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)
添加回答
举报
0/150
提交
取消