【发布时间】:2020-06-14 04:46:36
【问题描述】:
我写了自己的装饰器,它检查当前登录的用户是否是老师。如果没有,装饰器会将用户重定向到登录页面,但对于用户来说有点混乱,所以我想传递一条消息,为什么用户被重定向到登录页面,但我不知道该怎么做。
# here is my decorator:
def teacher_required(function=None, redirect_field_name=REDIRECT_FIELD_NAME, login_url='login'):
'''
Decorator for views that checks that the logged in user is a teacher,
redirects to the log-in page if necessary.
'''
actual_decorator = user_passes_test(
lambda u: u.is_active and u.is_teacher,
login_url=login_url,
redirect_field_name=redirect_field_name
)
if function:
return actual_decorator(function)
return actual_decorator
【问题讨论】:
标签: python django python-decorators