我有以下修饰器和视图哪些工作正常。Django添加可选参数给修饰器
装饰
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:
但我怎么能修改我的装饰等,它需要在一个额外的参数,像这样:
@event_admin_only(obj1,[...])
def event_dashboard(request, event_slug:
可能重复[如何创建一个可以使用或不使用参数的Python装饰器?](http://stackoverflow.com/questions/653368/how-to-create-a-python-decorator-可以使用的,无论是否带参数) – DrTyrsa 2012-01-27 07:47:47