【发布时间】:2015-04-18 13:40:06
【问题描述】:
我正在尝试为我的 django 应用程序编写一些中间件。在docs 中,它使用两个参数定义流程响应,请求和响应。但是当我尝试编写一些中间件时,会抛出异常:
Type Error:
process_response() takes 2 positional arguments but 3 were given
这是我的代码:
从 django.http 导入 HttpResponseRedirect
class Middleware(object):
def process_response(self, request, response):
print('processing response')
ref = request.GET.get('ref','')
if ref:
response.set_cookie('ref', ref, max_age=3600*24*365)
return response
def process_request(request):
print('processing request')
print(request.META['PATH_INFO'])
ref = request.COOKIES.get('ref','')
if ref:
request.GET['ref'] = ref
return None
但是后来发生了一件奇怪的事情。我注释掉了 process_request 函数。然后我得到0个错误。当我取消注释 process_request 函数时,我得到了错误:
process_request() takes 1 positional argument but 2 were given
这开始看起来像是随机行为..
【问题讨论】: