Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python process_request()只接受2个参数(给定1个)-Django Basic HTTP Auth Decorator_Python_Django - Fatal编程技术网

Python process_request()只接受2个参数(给定1个)-Django Basic HTTP Auth Decorator

Python process_request()只接受2个参数(给定1个)-Django Basic HTTP Auth Decorator,python,django,Python,Django,我的任务是创建一个基本的装饰器,我们可以用基本的HTTP身份验证装饰一些特定的django视图 我有以下的装饰师: def basic_auth(func): def process_request(self, request): if request.META.get("HTTP_AUTHORIZATION"): encoded_auth = request.META.get("HTTP_AUTHORIZATION")

我的任务是创建一个基本的装饰器,我们可以用基本的HTTP身份验证装饰一些特定的django视图

我有以下的装饰师:

def basic_auth(func):

    def process_request(self, request):
        if request.META.get("HTTP_AUTHORIZATION"):
            encoded_auth =  request.META.get("HTTP_AUTHORIZATION")
            encoded_auth = encoded_auth[6:]
            auth = base64.b64decode(encoded_auth)
            auth_email = auth.split(":")[0]
            auth_password = auth.split(":")[1]
            if (auth_email == settings.BASIC_AUTH_EMAIL) and (auth_password == settings.EMAIL_HOST_PASSWORD):
                func()
            else:
                return HttpResponseForbidden("Forbidden")
    return process_request
我用它来装饰这样的景色:

@csrf_exempt
@basic_auth
def user_find(request):
    args = json.loads(request.body, object_hook=utils._datetime_decoder)
    providedEmail = args['providedEmail']
    try:
        user = User.objects.get(email=providedEmail)
        user_dict = {'exists': 'true', 'name': user.first_name, 'email': user.email}
        return HttpResponse(json.dumps(user_dict))
    except User.DoesNotExist:
        user_dict = {'exists': 'false'} 
        return HttpResponse(json.dumps(user_dict))
由于某种原因,我没有得到我所期望的。视图在没有decorator的情况下可以完美地工作,但在没有decorator的情况下则无法工作。我得到了上面的错误:

process_request() takes exactly 2 arguments (1 given) 

您知道这里可能发生什么吗?

您没有将请求作为参数传递给func()。试试这个:

if (auth_email == settings.BASIC_AUTH_EMAIL) and (auth_password == settings.EMAIL_HOST_PASSWORD):
    func(request)
else:
    return HttpResponseForbidden("Forbidden")

将self从进程\u请求中删除,并将请求参数传递给func()

为了更好地理解函数装饰器:

def basic_auth(func):

    def process_request(request):
        if request.META.get("HTTP_AUTHORIZATION"):
            encoded_auth =  request.META.get("HTTP_AUTHORIZATION")
            encoded_auth = encoded_auth[6:]
            auth = base64.b64decode(encoded_auth)
            auth_email = auth.split(":")[0]
            auth_password = auth.split(":")[1]
            if (auth_email == settings.BASIC_AUTH_EMAIL) and (auth_password == settings.EMAIL_HOST_PASSWORD):
                func(request)
            else:
                return HttpResponseForbidden("Forbidden")
    return process_request