gpt4 book ai didi

Django:是否在URL分配器中应用权限?

转载 作者:行者123 更新时间:2023-12-04 05:34:49 30 4
gpt4 key购买 nike

在我的Django应用程序中,我具有用户访问某些 View 所需的某些权限(使用django.contrib.auth)。在我的 View 函数上使用@permission_required装饰器,效果很好。

但是,我的某些URL会解析为我未编写的 View ,例如内置的django.contrib.auth.views.password_change,如以下urls.py所示:

urlpatterns = patterns(
(r'^$', "users.views.index"),
(r'^password_change/$', 'django.contrib.auth.views.password_change'))

在这种情况下,我无处可使用 @permission_required装饰器-还是可以?有什么方法可以在URL调度程序级别应用权限限制?

最佳答案

可以导入所需的登录功能并将其应用于通用 View :

from django.contrib.auth.decorators import login_required
from django.views.generic.simple import direct_to_template
urlpatterns = patterns('',
(r'^foo/$', login_required(direct_to_template), {'template': 'foo_index.html'}),
)

提到 here

关于Django:是否在URL分配器中应用权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3461217/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com