我试图使用通配符技术在URL中为请求实现权限判断机制,而不是在每个视图上实现权限判断.

目前我拥有的是.

urlpatterns = [
    path('admin/', include('admin_urls.py')),
    ...
]

我的admin_urls.py分如下

urlpatterns = [
    path('', ViewSpaceIndex.as_view(), name="admin_index"),
     path('', EmployeeView.as_view(), name="employee"),
    ...
]

观点如下

@method_decorator(admin_required, name='dispatch')
 class EmployeeView(TemplateView):
     template_name = 'secret.html'



@method_decorator(admin_required, name='dispatch')
 class EmployeeView(TemplateView):
     template_name = 'secret.html'

我想要实现的是,不必在每个视图中重复使用@method_decorator(admin_required, name='dispatch') decorator,我想将许可应用于一个野生动物

  http.authorizeRequests()
           
            .antMatchers("/admin/**").has_permission("is_admin")
            

推荐答案

您可以在项目根url中这样做

from .my_custom_decorators import admin_required
urlpatterns = [
    path('admin/', admin_required(include('admin_urls.py'))),
    ...
]

我不知道这是否有效,但你可以试试

Django相关问答推荐

自定义公钥打破Django管理内联逻辑

身份验证有效,但登录无效.一直卡在pending

Django 仅预取相关模型的最新对象

Django - 使用在 URL 中传递的父类主键从子类中过滤对象

所有子元素的Django自递归外键过滤器查询

JSP模板继承

如何从 json 模式创建表单?

如何在 django 中处理这种竞争条件?

将网络测功机添加到 Heroku django 应用程序时如何克服Coudn't find that formation错误?

如何在 Django 中返回静态 HTML 文件作为响应?

Django:如何从模板调用视图函数?

在 virtualenv Ubuntu 12.10 中使用 pip 安装 lxml 错误:command 'gcc' failed with exit status 4

Django:使用 ModelForm 编辑现有数据库条目

__init__() 得到了一个意外的关键字参数user

如何以编程方式对 Django 中的用户进行身份验证?

Django 在视图之间传递数据

Heroku & Django:OSError: No such file or directory:'/app/{myappname}/static'

Django 中的自定义 HTTP 标头

Numpy 数组到 base64 并返回到 Numpy 数组 - Python

左加入 Django ORM