Django使用遇到的各种问题及解决方法
从Django的 搭建开始,遇到的问题就不断,网站还没有发布,就出错了,我查了好多资料,啃得了不少东西,也没有找到合适的方法,终于没办法了,自己硬着头皮往下读,终于解决了这些问题,下面分享给大家。
代码是这样的:
from wsgiref.simple_server import make_server def application(environ, start_response): start_response('200 OK', [('Content-Type', 'text/html')]) #return [b'<h1>Hello, web!</h1>'] return '<h1>hello,web</h1>' httpd = make_server(host='',port=8888,app =application) print('Serving HTTP on port 8888...')
第一步在运行程序,包括在cmd中运行如下代码:
python manage.py runserver
都出现出现了错误如下:
Performing system checks... System check identified no issues (0 silenced). You have 13 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions. Run 'python manage.py migrate' to apply them. July 07, 2017 - 14:04:53 Django version 1.11.3, using settings 'hello.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK. 127.0.0.1 Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x0383A6F0> Traceback (most recent call last): File "D:\Program Files\Python\Python36\lib\site-packages\django\utils\autoreload.py", line 227, in wrapper fn(*args, **kwargs) File "D:\Program Files\Python\Python36\lib\site-packages\django\core\management\commands\runserver.py", line 149, in inner_run ipv6=self.use_ipv6, threading=threading, server_cls=self.server_cls) File "D:\Program Files\Python\Python36\lib\site-packages\django\core\servers\basehttp.py", line 164, in run httpd = httpd_cls(server_address, WSGIRequestHandler, ipv6=ipv6) File "D:\Program Files\Python\Python36\lib\site-packages\django\core\servers\basehttp.py", line 74, in __init__ super(WSGIServer, self).__init__(*args, **kwargs) File "D:\Program Files\Python\Python36\lib\socketserver.py", line 453, in __init__ self.server_bind() File "D:\Program Files\Python\Python36\lib\wsgiref\simple_server.py", line 50, in server_bind HTTPServer.server_bind(self) File "D:\Program Files\Python\Python36\lib\http\server.py", line 138, in server_bind self.server_name = socket.getfqdn(host) File "D:\Program Files\Python\Python36\lib\socket.py", line 674, in getfqdn hostname, aliases, ipaddrs = gethostbyaddr(name) UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd2 in position 0: invalid continuation byte
View Code
究其意思就是编码错误:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd2 in position 0: invalid continuation byte
Python3默认的是utf-8编码,而我们最苦逼的地方就是中文,程序遇到中文极大可能性会报错。出现编码问题,说明解码方式不对,可能是utf8解码中文出错,接着确认哪里出了问题。错误提示发现是
hostname, aliases, ipaddrs = gethostbyaddr(name)
这句代码出了错误,这句代码是个函数,函数有参数,那先从参数入手,参数是name,那可能name是个中文。经研究错误提示发现gethostbyaddr()函数是中文翻译就是获取主机地址,而传参是名字,那么name传入的就是主机名,也就是我的电脑名。我的电脑名是中文,是不是改成英文就可以了,经测试发现的确是主机中文名导致的问题,改成英文名即可顺利启动本地服务器。
!!!!!!!!!!但是
启动之后又错了,报错内容如下:
Serving HTTP on port 8888... 127.0.0.1 - - [19/Dec/2017 11:03:06] "GET / HTTP/1.1" 200 0 Traceback (most recent call last): File "D:\python3\lib\wsgiref\handlers.py", line 138, in run self.finish_response() File "D:\python3\lib\wsgiref\handlers.py", line 180, in finish_response self.write(data) File "D:\python3\lib\wsgiref\handlers.py", line 266, in write "write() argument must be a bytes instance" AssertionError: write() argument must be a bytes instance 127.0.0.1 - - [19/Dec/2017 11:03:06] "GET / HTTP/1.1" 500 59 ---------------------------------------- Exception happened during processing of request from ('127.0.0.1', 50749) Traceback (most recent call last): File "D:\python3\lib\wsgiref\handlers.py", line 138, in run self.finish_response() File "D:\python3\lib\wsgiref\handlers.py", line 180, in finish_response self.write(data) File "D:\python3\lib\wsgiref\handlers.py", line 266, in write "write() argument must be a bytes instance" AssertionError: write() argument must be a bytes instance During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\python3\lib\wsgiref\handlers.py", line 141, in run self.handle_error() File "D:\python3\lib\wsgiref\handlers.py", line 368, in handle_error self.finish_response() File "D:\python3\lib\wsgiref\handlers.py", line 180, in finish_response self.write(data) File "D:\python3\lib\wsgiref\handlers.py", line 274, in write self.send_headers() File "D:\python3\lib\wsgiref\handlers.py", line 331, in send_headers if not self.origin_server or self.client_is_modern(): File "D:\python3\lib\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 "D:\python3\lib\socketserver.py", line 317, in _handle_request_noblock self.process_request(request, client_address) File "D:\python3\lib\socketserver.py", line 348, in process_request self.finish_request(request, client_address) File "D:\python3\lib\socketserver.py", line 361, in finish_request self.RequestHandlerClass(request, client_address, self) File "D:\python3\lib\socketserver.py", line 696, in __init__ self.handle() File "D:\python3\lib\wsgiref\simple_server.py", line 133, in handle handler.run(self.server.get_app()) File "D:\python3\lib\wsgiref\handlers.py", line 144, in run self.close() File "D:\python3\lib\wsgiref\simple_server.py", line 35, in close self.status.split(' ',1)[0], self.bytes_sent AttributeError: 'NoneType' object has no attribute 'split' ---------------------------------------- 127.0.0.1 - - [19/Dec/2017 11:04:06] "GET / HTTP/1.1" 200 0 Traceback (most recent call last): File "D:\python3\lib\wsgiref\handlers.py", line 138, in run self.finish_response() File "D:\python3\lib\wsgiref\handlers.py", line 180, in finish_response self.write(data) File "D:\python3\lib\wsgiref\handlers.py", line 266, in write "write() argument must be a bytes instance" AssertionError: write() argument must be a bytes instance 127.0.0.1 - - [19/Dec/2017 11:04:06] "GET / HTTP/1.1" 500 59 ---------------------------------------- Exception happened during processing of request from ('127.0.0.1', 50835) Traceback (most recent call last): File "D:\python3\lib\wsgiref\handlers.py", line 138, in run self.finish_response() File "D:\python3\lib\wsgiref\handlers.py", line 180, in finish_response self.write(data) File "D:\python3\lib\wsgiref\handlers.py", line 266, in write "write() argument must be a bytes instance" AssertionError: write() argument must be a bytes instance During handling of the above exception, another exception occurred: Traceback (most recent call last): File "D:\python3\lib\wsgiref\handlers.py", line 141, in run self.handle_error() File "D:\python3\lib\wsgiref\handlers.py", line 368, in handle_error self.finish_response() File "D:\python3\lib\wsgiref\handlers.py", line 180, in finish_response self.write(data) File "D:\python3\lib\wsgiref\handlers.py", line 274, in write self.send_headers() File "D:\python3\lib\wsgiref\handlers.py", line 331, in send_headers if not self.origin_server or self.client_is_modern(): File "D:\python3\lib\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 "D:\python3\lib\socketserver.py", line 317, in _handle_request_noblock self.process_request(request, client_address) File "D:\python3\lib\socketserver.py", line 348, in process_request self.finish_request(request, client_address) File "D:\python3\lib\socketserver.py", line 361, in finish_request self.RequestHandlerClass(request, client_address, self) File "D:\python3\lib\socketserver.py", line 696, in __init__ self.handle() File "D:\python3\lib\wsgiref\simple_server.py", line 133, in handle handler.run(self.server.get_app()) File "D:\python3\lib\wsgiref\handlers.py", line 144, in run self.close() File "D:\python3\lib\wsgiref\simple_server.py", line 35, in close self.status.split(' ',1)[0], self.bytes_sent AttributeError: 'NoneType' object has no attribute 'split' ----------------------------------------
View Code
自己也继续看,因为百度啊,google啊,私信请教人啊,均无收获,只能自己看,首先根据提示第一个错误就是
Assertion Error: write() argument must be a bytes instance
解决办法如下:
return ['<h1>Hello world</h1>'.encode('utf-8'),]
后面再遇到问题的话,小编会相继传上去的,