平台:win10
工具:cmd python3
刚刚学习Django搭建环境,网站还木有发布,就直接来了个大麻烦。
在最后cmd运行服务器(manage runserver 或者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 FilesPythonPython36libsite-packagesdjangoutilsautoreload.py", line 227, in wrapper fn(*args, **kwargs) File "D:Program FilesPythonPython36libsite-packagesdjangocoremanagementcommands unserver.py", line 149, in inner_run ipv6=self.use_ipv6, threading=threading, server_cls=self.server_cls) File "D:Program FilesPythonPython36libsite-packagesdjangocoreserversasehttp.py", line 164, in run httpd = httpd_cls(server_address, WSGIRequestHandler, ipv6=ipv6) File "D:Program FilesPythonPython36libsite-packagesdjangocoreserversasehttp.py", line 74, in __init__ super(WSGIServer, self).__init__(*args, **kwargs) File "D:Program FilesPythonPython36libsocketserver.py", line 453, in __init__ self.server_bind() File "D:Program FilesPythonPython36libwsgirefsimple_server.py", line 50, in server_bind HTTPServer.server_bind(self) File "D:Program FilesPythonPython36libhttpserver.py", line 138, in server_bind self.server_name = socket.getfqdn(host) File "D:Program FilesPythonPython36libsocket.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
反正就是编码错误: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传入的就是主机名,也就是我们电脑名。我的电脑名是中文,是不是改成英文就可以了,经测试发现的确是主机中文名导致的问题,改成英文名即可顺利启动本地服务器。
把电脑名称改为英文就可以解决了