装饰器工作正常,但如果用户不属于任何所需的组,我想显示一条错误消息(我想使用消息框架)。这是装饰器:
def group_required(*group_names):
"""Requires user membership in at least one of the groups passed in."""
def in_groups(user):
if user.is_authenticated():
if bool(user.groups.filter(name__in=group_names)) or user.is_superuser:
return True
return False
return user_passes_test(in_groups)
我用类似的东西来称呼它:
@require_http_methods(['GET'])
@group_required('supervisor')
def home_view(request):
return render(request, 'home.html')
我尝试使用此代码段来使用消息框架(因为这需要请求对象),但它意识到消息框架中间件并未安装在装饰器中。
我愿意改变一切:)
更新:
我在找什么:
def group_required(request, *group_names):
"""Requires user membership in at least one of the groups passed in."""
def in_groups(user):
if user.is_authenticated():
if user.groups.filter(name__in=group_names).exists() or user.is_superuser:
return True
else:
# I'm getting:
# You cannot add messages without installing django.contrib.messages.middleware.MessageMiddleware
messages.add_message(request, messages.ERROR, 'Group is not allowed')
return False
return user_passes_test(in_groups, request)