【发布时间】:2021-05-12 10:26:33
【问题描述】:
我在视图中使用 mixins 来限制未经身份验证的用户访问我的博客文章页面、编辑页面和删除页面。现在只有管理员用户可以访问这些页面,但我也授予员工用户访问这些页面的权限。看图片:
我的员工用户在尝试访问这些页面时仍然收到 404 错误。这是我的代码:
#views.py
class blog_publish_view(PermissionRequiredMixin,CreateView):
raise_exception = True
permission_required = "blog_add_blog"
model = Post
form_class = BlogPost
template_name = "blog_post.html"
#fields = ['title','author','body']
class blog_update_view(PermissionRequiredMixin,UpdateView):
raise_exception = True
permission_required = "blog_change_blog"
model = Post
template_name = "blog_update_post.html"
form_class = BlogPost
class blog_delete_view(PermissionRequiredMixin,DeleteView):
raise_exception = True
permission_required = "blog_delete_blog"
model = Post
template_name = "delete_blog_post.html"
success_url = reverse_lazy('blog')
#用于发布博客的 html 页面:
{% if user.is_authenticated %}
<div class="container">
<h1>Publish Blog</h1>
<form method="POST">
{% csrf_token %}
{{form.media}}
{{form.as_p}}
<button class="btn btn-info">Publish</button>
</form>
</div>
{% endif %}
我的员工用户可以从 django 管理面板发布、编辑、发布博客,但如何在 html 模板中启用这些权限。
【问题讨论】:
-
不是permission_required = "blog_change_post" 吗?还是包括帖子在内的东西?