小编Shi*_*rma的帖子

Django - 视图accounts.decorators.wrapper_function 未返回HttpResponse 对象。它返回 None 相反

我正在构建一个客户管理应用程序,并且已经构建了一些装饰器。但是当我运行应用程序时出现此错误

视图accounts.decorators.wrapper_function没有返回HttpResponse对象。它返回 None 相反。

装饰器.py

from django.http import HttpResponse
from django.shortcuts import redirect


def unauthenticated_user(view_func):
    def wrapper_func(request, *args, **kwargs):
        if request.user.is_authenticated:
            return redirect('home')
        elif request.user.is_authenticated == None:
            return redirect('login')
        else:
            return view_func(request, *args, **kwargs)

    return wrapper_func


def allowed_users(allowed_roles=[]):
    def decorator(view_func):
        def wrapper_func(request, *args, **kwargs):

            group = None
            if request.user.groups.exists():
                group = request.user.groups.all()[0].name

            if group in allowed_roles:
                return view_func(request, *args, **kwargs)
            else:
                return HttpResponse('You are not authorized to view this page')
        return wrapper_func
    return decorator


def admin_only(view_func):
    def wrapper_function(request, *args, **kwargs): …
Run Code Online (Sandbox Code Playgroud)

django django-models django-views python-decorators

2
推荐指数
1
解决办法
6843
查看次数