简体   繁体   中英

Django: Custom Middleware results in NoneType Error

i got this Error:

Internal Server Error: /
    Traceback (most recent call last):
      File "/home/cena/AjiiMajii/.venv/local/lib/python2.7/site-packages/django/core/handlers/exception.py", line 41, in inner
        response = get_response(request)
      File "/home/cena/AjiiMajii/.venv/local/lib/python2.7/site-packages/django/utils/deprecation.py", line 142, in __call__
        response = self.process_response(request, response)
      File "/home/cena/AjiiMajii/.venv/local/lib/python2.7/site-packages/django/middleware/clickjacking.py", line 32, in process_response
        if response.get('X-Frame-Options') is not None:
    AttributeError: 'NoneType' object has no attribute 'get'
    [04/Dec/2017 20:08:25] "GET / HTTP/1.1" 500 65880

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'cms.Middleware.Visits',
]

</pre>

and my custom middleware 'cms.Middleware.Visits'

class Visits(MiddlewareMixin): def __init__(self,get_response): self.get_response = get_response def __call__(self, request): print '*'*22 print self.get_client_ip(request) print '*'*22 def get_client_ip(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') return ip def RedisConnection(self): Connection=redis.Redis(host='localhost',port='6379') self.Connection=Connection return self.Connection def IpCheker(self): # just set and count of visit[s] for ever ip print '%'*30 print self.get_client_ip() print '%'*30 self.RedisConnection() if self.Connection.get(self.CliReq): HashPattern=self.HashSetter(self.CliReq) UserId=self.Connection.get(self.CliReq) UserPattern="User:Id:{}".format(UserId) print UserPattern self.Connection.hincrby(UserPattern,'vcount') else: IDnum=self.IdGenerator(self.CliReq) self.Connection.set(self.CliReq,IDnum) HashPattern=self.HashSetter(self.CliReq) Location=self.GetLoc(self.CliReq) self.Connection.hset(HashPattern,'ip',self.CliReq) self.Connection.hset(HashPattern,'vcount',1) self.Connection.hset(HashPattern,'loc',Location) def HashSetter(self,ip): id=self.IdGenerator(ip) IdStyle='User:Id:{}'.format(id) return IdStyle def IdGenerator(self,ip): Count=self.Connection.keys('User:Id:*') id= len(Count) + 1 return id def GetLoc(self,ip): #GetLocation oF ip's print 'f' * 80 ApiLoc='http://www.freegeoip.net/json/' ABSLOC=requests.get('http://www.freegeoip.net/json/{}'.format(ip)) LocInfo=json.loads(ABSLOC.content) return LocInfo['country_name'] </pre>

i cant resolve or debug this, why this happen?

I think in the middleware call method you should return the response. Please check the document for reference:

Writing custom middleware

The error seems to happen since you are not returning the response in the call () method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM