我有以下的装饰和看法,工作良好。
装饰工
def event_admin_only(func):
"""
Checks if the current role for the user is an Event Admin or not
"""
def decorator(request, *args, **kwargs):
event = get_object_or_404(Event, slug=kwargs['event_slug'])
allowed_roles = [role[1] for role in Role.ADMIN_ROLES]
# get user current role
current_role = request.session.get('current_role')
if current_role not in allowed_roles:
url = reverse('no_perms')
return redirect(url)
else:
return func(request, *args, **kwargs)
return decorator
查看
@event_admin_only
def event_dashboard(request, event_slug):
pass
但是我如何修改decorator,使其接受一个额外的参数,如:
@event_admin_only(obj1,[...])
def event_dashboard(request, event_slug):
pass
您需要将decorator函数的创建包装到另一个函数中:
可以这样称呼:
我建议e-satis对这个问题的回答是为了理解这一点:How to make a chain of function decorators?
相关问题 更多 >
编程相关推荐