如何在Django中定义权限而无需人员状态?
我想授予某些用户创建文章的权限,而不给他们分配人员状态 原因是我不想让任何用户访问管理面板进行身份验证,就像向视图添加如何在Django中定义权限而无需人员状态?,django,django-permissions,Django,Django Permissions,我想授予某些用户创建文章的权限,而不给他们分配人员状态 原因是我不想让任何用户访问管理面板进行身份验证,就像向视图添加@staff\u member\u required时发生的那样。相反,我只想添加一个/add/article链接到拥有此类权限的用户的配置文件 如何实现这一点?如果您使用的是传统的功能视图,可以执行以下操作: def add_article(request): if not request.user.has_perm('articles.add_article'):
@staff\u member\u required
时发生的那样。相反,我只想添加一个/add/article
链接到拥有此类权限的用户的配置文件
如何实现这一点?如果您使用的是传统的功能视图,可以执行以下操作: def add_article(request): if not request.user.has_perm('articles.add_article'): return HttpResponseForbidden() # Now only users that have the permission will reach this # so add your normal view handling {% if perms.articles.add_article %} Show url for article editing only to users that have the rpmission {% endif %} 如果您使用的是CBV,那么您应该将CBV的
方法修饰为_view()
方法(在url.py
中),或者使用django括号()中的PermissionRequiredMixin
,或者在CBV的dispatch()
方法中执行您自己的检查
另外,在模板中,将添加文章url放在烫发检查中,如下所示:
def add_article(request):
if not request.user.has_perm('articles.add_article'):
return HttpResponseForbidden()
# Now only users that have the permission will reach this
# so add your normal view handling
{% if perms.articles.add_article %}
Show url for article editing only to users that have the rpmission
{% endif %}
{%if perms.articles.add_article%}
仅向具有权限的用户显示文章编辑的url
{%endif%}
我使用过渡函数视图。帮了大忙。谢谢瑟拉费姆。