简体   繁体   English

在Django装饰器中使用user_passes_test时,如何重定向login_url以外的URL?

[英]How to redirect url other than login_url when using user_passes_test in django decorators?

Here is my code: 这是我的代码:

url(r'^permission_not_granted/',views.permission_not_granted, name='permission_not_granted'),

this is inside decorators.py: def is_admin_or_senior_surveyor(user): return user.userrole.id == 1 这是在decorators.py内部:def is_admin_or_senior_surveyor(user):返回user.userrole.id == 1

this is the view method : 这是视图方法:

def permission_not_granted(request):
    return HttpResponse("Permission Not Granted")

Here is my permission: 这是我的允许:

@user_passes_test(is_admin_or_senior_surveyor,login_url=None,redirect_field_name='/permission_not_granted/')

You should use login_url to specify the URL you want to redirect to: 您应该使用login_url指定要重定向到的URL:

@user_passes_test(is_admin_or_senior_surveyor, login_url='/permission_not_granted/')

You can also use the URL pattern name: 您还可以使用URL模式名称:

@user_passes_test(is_admin_or_senior_surveyor, login_url='permission_not_granted')

The redirect_field_name argument is used in the querystring eg /permission_not_granted?next=/protected-url/ . redirect_field_name参数用于查询字符串,例如/permission_not_granted?next=/protected-url/ This allows Django to redirect to the protected URL after the user has logged in on the login page. 这使Django在用户登录登录页面后可以重定向到受保护的URL。 It defaults to next , and you shouldn't usually have to change it. 它默认为next ,通常不需要更改它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM