我需要修改中间件中的响应对象,所以我添加了“myproject.common.middlware.ResponseMiddleware”
MIDDLEWARE_CLASSES = (
'django.contrib.sessions.middleware.SessionMiddleware', # order matters don't move
'django.middleware.gzip.GZipMiddleware', # order matters don't move
'django.middleware.locale.LocaleMiddleware', # order matters don't move
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'myproject.common.middleware.ResponseMiddleware',
)
但是在我的 ResponseMiddleware 中,我有以下代码:
class ResponseMiddleware(object):
def process_response(self, request, response):
if 'status_code' not in response:
response['status_code'] = 200
return response
但是在django的CommonMiddleware等其他中间件中找不到response.status_code。我使用 Django 1.4 并且从文档中,到 process_response 的顺序与 MIDDLEWARE_CLASSES 中定义的顺序相反。所以我把我的放在最底层。
我明白了
AttributeError:“dict”对象没有来自其他中间件的属性“status_code”。
(***这与 WebSocket在django common middleware中产生错误有关)