照着教程想写个简易的django登录验证试试中间件,也在settings里注册过了,之前简单试了一下确认了是有用的(不是这个登录验证),但是改成登录验证后无论是已登录还是未登录的判定都没生效(有cookies和清除cookies),也就是没有返回httpresponse
下图是我写的代码
函数名是固定的,你写的不对,应该是process_request,你写的process_requset
有帮助的话,请点采纳该答案~
Exception happened during processing of request from (‘127.0.0.1’, 57298)
Traceback (most recent call last):
File “/usr/local/python3.4/lib/python3.4/wsgiref/handlers.py”, line 138, in run
self.finish_response()
File “/usr/local/python3.4/lib/python3.4/wsgiref/handlers.py”, line 180, in finish_response
self.write(data)
File “/usr/local/python3.4/lib/python3.4/wsgiref/handlers.py”, line 274, in write
self.send_headers()
File “/usr/local/python3.4/lib/python3.4/wsgiref/handlers.py”, line 333, in send_headers
self._write(bytes(self.headers))
File “/usr/local/python3.4/lib/python3.4/wsgiref/headers.py”, line 142, in bytes
return str(self).encode(‘iso-8859-1’)
UnicodeEncodeError: ‘latin-1’ codec can’t encode characters in position 276-277: ordinal not in range(256)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “/usr/local/python3.4/lib/python3.4/wsgiref/handlers.py”, line 141, in run
self.handle_error()
File “/home/python/.virtualenvs/django_py3.4/lib/python3.4/site-packages/django/core/servers/basehttp.py”, line 95, in handle_error
super(ServerHandler, self).handle_error()
File “/usr/local/python3.4/lib/python3.4/wsgiref/handlers.py”, line 368, in handle_error
self.finish_response()
File “/usr/local/python3.4/lib/python3.4/wsgiref/handlers.py”, line 180, in finish_response
self.write(data)
File “/usr/local/python3.4/lib/python3.4/wsgiref/handlers.py”, line 274, in write
self.send_headers()
File “/usr/local/python3.4/lib/python3.4/wsgiref/handlers.py”, line 331, in send_headers
if not self.origin_server or self.client_is_modern():
File “/usr/local/python3.4/lib/python3.4/wsgiref/handlers.py”, line 344, in client_is_modern
return self.environ[‘SERVER_PROTOCOL’].upper() != ‘HTTP/0.9’
TypeError: ‘NoneType’ object is not subscriptable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “/usr/local/python3.4/lib/python3.4/socketserver.py”, line 617, in process_request_thread
self.finish_request(request, client_address)
File “/usr/local/python3.4/lib/python3.4/socketserver.py”, line 344, in finish_request
self.RequestHandlerClass(request, client_address, self)
File “/home/python/.virtualenvs/django_py3.4/lib/python3.4/site-packages/django/core/servers/basehttp.py”, line 102, in init
super(WSGIRequestHandler, self).init(*args, **kwargs)
File “/usr/local/python3.4/lib/python3.4/socketserver.py”, line 673, in init
self.handle()
File “/home/python/.virtualenvs/django_py3.4/lib/python3.4/site-packages/django/core/servers/basehttp.py”, line 182, in handle
handler.run(self.server.get_app())
File “/usr/local/python3.4/lib/python3.4/wsgiref/handlers.py”, line 144, in run
self.close()
File “/usr/local/python3.4/lib/python3.4/wsgiref/simple_server.py”, line 35, in close
self.status.split(’ ',1)[0], self.bytes_sent
AttributeError: ‘NoneType’ object has no attribute ‘split’
解决方法:
用json.dumps(value)成功。
需要用到cookie中的内容时再 json.loads(value)就可以