django 编码的坑

平台: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 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传入的就是主机名,也就是我们电脑名。我的电脑名是中文,是不是改成英文就可以了,经测试发现的确是主机中文名导致的问题,改成英文名即可顺利启动本地服务器。

把电脑名称改为英文就可以解决了

转载于:https://www.cnblogs.com/wts-home/p/9621421.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值