Python中间件对特定需求的调用

2024-09-29 00:21:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在实现自定义中间件,它将检查每个请求的令牌身份验证我希望为每个请求运行此命令,并接受一个请求

class CheckAuthorization(object):
    def process_request(self, request):
        getKey = request.POST.get('authKey')
        if getKey is not None and getKey != '':
            try:
                auth = TblAutherization.objects.get(secret_key = request.POST.get('authKey'))
            except TblAutherization.DoesNotExist:
                response = JsonResponse({'Status':'Error','Response code': 107,'Message':'Invalid Request'})
                return HttpResponse(response, content_type='application/json')
        else:
            response = JsonResponse({'Status':'Error','Response code': 105,'Message':'Missing Paramters'})
            return HttpResponse(response, content_type='application/json')

我不希望为一个特定的请求调用这个中间件,这是怎么可能的。如果你能指导我用其他方法做同样的事。我对Python不熟悉

谢谢


Tags: 中间件messagegetreturnresponserequeststatuscode
1条回答
网友
1楼 · 发布于 2024-09-29 00:21:52

要忽略特定的url,可以在process_request的开头添加一个检查,下面是一个示例代码:

def process_request(self, request):
    full_path = request.get_full_path()
    if full_path.startswith('/users/login'):
        return

相关问题 更多 >