平台:win10
工具:cmd python3
剛剛學習Django搭建環境,網站還木有發布,就直接來了個大麻煩。
一切按着《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 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
反正就是編碼錯誤: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傳入的就是主機名,也就是我們電腦名。我的電腦名是中文,是不是改成英文就可以了,經測試發現的確是主機中文名導致的問題,改成英文名即可順利啟動本地服務器。
系列下一篇:Django 踩過的坑(二)