中间件:'WSGIRequest'对象没有属性'get'

时间:2014-05-02 06:49:56

标签: python django django-middleware

我尝试使用中间件请求访问IP地址,这是我的示例代码,

def process_request(self, request):
    x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
    if x_forwarded_for:
       ip = x_forwarded_for.split(',')[-1].strip()
    else:
       ip = request.META.get('REMOTE_ADDR') --> I am getting the error here
    return ip

这是我的完整错误跟踪:

Environment:

Request Method: GET
Request URL: http://localhost/admin/management/banipaddress/

Django Version: 1.6.4
Python Version: 2.7.3
Installed Applications:
('django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'utils',
 'catalog',
 'preview',
 'cart',
 'management')
Installed Middleware:
('django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'custom.middleware.banipmiddleware.BanIpMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware')


Traceback:
File "/home/user/prodjango-env/venv/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
  201.                 response = middleware_method(request, response)
File "/home/user/prodjango-env/venv/local/lib/python2.7/site-packages/django/middleware/clickjacking.py" in process_response
  30.         if response.get('X-Frame-Options', None) is not None:

Exception Type: AttributeError at /admin/management/banipaddress/
Exception Value: 'WSGIRequest' object has no attribute 'get'

1 个答案:

答案 0 :(得分:2)

错误似乎在另一行

File "/home/user/prodjango-env/venv/local/lib/python2.7/site-packages/django/middleware/clickjacking.py" in process_response
  30.         if response.get('X-Frame-Options', None) is not None:
clickjacking.py 中的

不要知道你的。试图访问不正确的response.get()