【发布时间】:2021-08-02 20:45:03
【问题描述】:
现在我所有的 django 员工用户都可以编辑或删除其他员工用户的帖子。我希望他们只能从 django 管理面板编辑或删除自己的帖子。如何限制他们编辑或删除其他用户帖子?这是我的代码:
views.py:
class BlogPublishView(PermissionRequiredMixin,CreateView):
raise_exception = True
permission_required = "blog.add_post"
model = Post
form_class = BlogPost
template_name = "blog_post.html"
#fields = ['title','author','body']
class BlogUpdateView(PermissionRequiredMixin,UpdateView):
raise_exception = True
permission_required = "blog.change_post"
model = Post
template_name = "blog_update_post.html"
form_class = BlogPost
class BlogDeleteView(PermissionRequiredMixin,DeleteView):
raise_exception = True
permission_required = "blog.delete_post"
model = Post
template_name = "delete_blog_post.html"
success_url = reverse_lazy('blog')
urls.py
path('blog-post', BlogPublishView.as_view(), name='blog-post'),
path('blog-update/<slug:slug>', BlogUpdateView.as_view(), name='blog-update'),
path('blog-delete/<slug:slug>', BlogDeleteView.as_view(), name='blog-delete'),
html
{% if user.is_authenticated %}{% if user.id == post.author.id %} <a href="{% url 'blog-update' post.slug %}"><b>(Edit Blog)</b></a> <a href="{% url 'blog-delete' post.slug %}"><b>(Delete Blog)</b> </a>{% endif %}{% endif %}
如果你现在还不明白我的问题,让你再解释一下。假设我的 djano 管理面板“A”、“B”和“C”中有三个用户。用户“A”是管理员,用户“B”和“C”是员工用户。用户“B”和“C”只有从管理面板编辑、删除和发布帖子的权限。问题是用户“A”可以编辑和删除用户“B”的帖子,用户“B”也可以编辑或删除用户“A”的帖子。我想限制两个员工用户从 django 管理面板编辑、删除和查看彼此的帖子。他们只能从 django 管理面板查看、编辑和删除自己的帖子。
【问题讨论】: