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=,app =application) print('Serving HTTP on port 8888...')
第一步在运行程序,包括在cmd中运行如下代码:
python manage.py runserver
都出现出现了错误如下:
Performing system checks... System check identified no issues ( silenced). You have 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 , - ::
Django version 1.11., 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 , in wrapper
fn(*args, **kwargs)
File "D:\Program Files\Python\Python36\lib\site-packages\django\core\management\commands\runserver.py", line , 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 , 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 , in __init__
super(WSGIServer, self).__init__(*args, **kwargs)
File "D:\Program Files\Python\Python36\lib\socketserver.py", line , in __init__
self.server_bind()
File "D:\Program Files\Python\Python36\lib\wsgiref\simple_server.py", line , in server_bind
HTTPServer.server_bind(self)
File "D:\Program Files\Python\Python36\lib\http\server.py", line , in server_bind
self.server_name = socket.getfqdn(host)
File "D:\Program Files\Python\Python36\lib\socket.py", line , 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传入的就是主机名,也就是我的电脑名。我的电脑名是中文,是不是改成英文就可以了,经测试发现的确是主机中文名导致的问题,改成英文名即可顺利启动本地服务器。
!!!!!!!!!!但是
启动之后又错了,报错内容如下:
Serving HTTP on port ...
127.0.0.1 - - [/Dec/ ::] "GET / HTTP/1.1"
Traceback (most recent call last):
File "D:\python3\lib\wsgiref\handlers.py", line , in run
self.finish_response()
File "D:\python3\lib\wsgiref\handlers.py", line , in finish_response
self.write(data)
File "D:\python3\lib\wsgiref\handlers.py", line , in write
"write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance
127.0.0.1 - - [/Dec/ ::] "GET / HTTP/1.1"
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', )
Traceback (most recent call last):
File "D:\python3\lib\wsgiref\handlers.py", line , in run
self.finish_response()
File "D:\python3\lib\wsgiref\handlers.py", line , in finish_response
self.write(data)
File "D:\python3\lib\wsgiref\handlers.py", line , 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 , in run
self.handle_error()
File "D:\python3\lib\wsgiref\handlers.py", line , in handle_error
self.finish_response()
File "D:\python3\lib\wsgiref\handlers.py", line , in finish_response
self.write(data)
File "D:\python3\lib\wsgiref\handlers.py", line , in write
self.send_headers()
File "D:\python3\lib\wsgiref\handlers.py", line , in send_headers
if not self.origin_server or self.client_is_modern():
File "D:\python3\lib\wsgiref\handlers.py", line , 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 , in _handle_request_noblock
self.process_request(request, client_address)
File "D:\python3\lib\socketserver.py", line , in process_request
self.finish_request(request, client_address)
File "D:\python3\lib\socketserver.py", line , in finish_request
self.RequestHandlerClass(request, client_address, self)
File "D:\python3\lib\socketserver.py", line , in __init__
self.handle()
File "D:\python3\lib\wsgiref\simple_server.py", line , in handle
handler.run(self.server.get_app())
File "D:\python3\lib\wsgiref\handlers.py", line , in run
self.close()
File "D:\python3\lib\wsgiref\simple_server.py", line , in close
self.status.split(' ',)[], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
----------------------------------------
127.0.0.1 - - [/Dec/ ::] "GET / HTTP/1.1"
Traceback (most recent call last):
File "D:\python3\lib\wsgiref\handlers.py", line , in run
self.finish_response()
File "D:\python3\lib\wsgiref\handlers.py", line , in finish_response
self.write(data)
File "D:\python3\lib\wsgiref\handlers.py", line , in write
"write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance
127.0.0.1 - - [/Dec/ ::] "GET / HTTP/1.1"
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', )
Traceback (most recent call last):
File "D:\python3\lib\wsgiref\handlers.py", line , in run
self.finish_response()
File "D:\python3\lib\wsgiref\handlers.py", line , in finish_response
self.write(data)
File "D:\python3\lib\wsgiref\handlers.py", line , 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 , in run
self.handle_error()
File "D:\python3\lib\wsgiref\handlers.py", line , in handle_error
self.finish_response()
File "D:\python3\lib\wsgiref\handlers.py", line , in finish_response
self.write(data)
File "D:\python3\lib\wsgiref\handlers.py", line , in write
self.send_headers()
File "D:\python3\lib\wsgiref\handlers.py", line , in send_headers
if not self.origin_server or self.client_is_modern():
File "D:\python3\lib\wsgiref\handlers.py", line , 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 , in _handle_request_noblock
self.process_request(request, client_address)
File "D:\python3\lib\socketserver.py", line , in process_request
self.finish_request(request, client_address)
File "D:\python3\lib\socketserver.py", line , in finish_request
self.RequestHandlerClass(request, client_address, self)
File "D:\python3\lib\socketserver.py", line , in __init__
self.handle()
File "D:\python3\lib\wsgiref\simple_server.py", line , in handle
handler.run(self.server.get_app())
File "D:\python3\lib\wsgiref\handlers.py", line , in run
self.close()
File "D:\python3\lib\wsgiref\simple_server.py", line , in close
self.status.split(' ',)[], self.bytes_sent
AttributeError: 'NoneType' object has no attribute 'split'
----------------------------------------
自己也继续看,因为百度啊,google啊,私信请教人啊,均无收获,只能自己看,首先根据提示第一个错误就是
Assertion Error: write() argument must be a bytes instance
解决办法如下:
return ['<h1>Hello world</h1>'.encode('utf-8'),]
后面再遇到问题的话,小编会相继传上去的,
Django使用遇到的各种问题及解决方法的更多相关文章
- ##Django中Application labels aren't unique解决方法##
pip更新了所有插件,发现了按平常编码遇到些问题,记录下. Django错误 django.core.exceptions.ImproperlyConfigured: Application labe ...
- Django与Vue语法冲突问题完美解决方法
当我们在django web框架中,使用vue的时候,会遇到语法冲突. 因为vue使用{{}},而django也使用{{}},因此会冲突. 解决办法1: 在django1.5以后,加入了标签: {% ...
- django 渲染模板与 vue 的 {{ }} 冲突解决方法
如果不可避免的在同一个页面里既有 django 渲染又有 vue 渲染的部分,可有 2 种方式解决 方法一: 采用 vue 的 delimiters 分隔符. new Vue({ delimiter ...
- Django 1.10 找不到静态资源解决方法
测试版本:Django 1.10 问题:Django项目找不到静态资源 解决方法: 1.首先你需要在自己的app下面创建2个目录 static 和 templates 树形结构如下(DjangoPr ...
- Django的POST请求时因为开启防止csrf,报403错误,及四种解决方法
Django默认开启防止csrf(跨站点请求伪造)攻击,在post请求时,没有上传 csrf字段,导致校验失败,报403错误 解决方法1: 注释掉此段代码,即可. 缺点:导致Django项目完全无法防 ...
- Django忘记管理员账号和密码的解决办法
看着Django的教程学习搭建网站,结果忘记第一次创建的账号和密码了.结果搭建成功以后,一直无法登陆到管理页面,进行不下去了. 如图所示: 在网上找了很多的方法都不行,最后使用新建一个superuse ...
- 使用django建博客时遇到的URLcon相关错误以及解决方法。错误提示:类型错误:include0获得一个意外的关键参数app_name
root@nanlyvm:/home/mydj/mysite# python manage.py runserver Performing system checks... Unhandled exc ...
- Django的admin管理系统写入中文出错的解决方法/1267 Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and (utf8_general_ci,COERCIBLE) for operation ‘locate’
Django的admin管理系统写入中文出错的解决方法 解决错误: 1267 Illegal mix of collations (latin1_swedish_ci,IMPLICIT) and ( ...
- django框架使用mysql报错,及两种解决方法
1.django框架 settings.py文件中部分代码: DATABASES = { # 'default': { # 'ENGINE': 'django.db.backends.sqlite3' ...
随机推荐
- Count the Colors
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Subm ...
- AngularJS学习篇(十六)
AngularJS 表单 HTML 控件 以下 HTML input 元素被称为 HTML 控件: input 元素 select 元素 button 元素 textarea 元素 HTML 表单 H ...
- box-shadow + animation 实现loading
.loading{ width:3px; height:3px; border-radius:100%; margin-left:20px; box-shadow:0 -10px 0 1px #333 ...
- ajax事件请求
首先,ajax是什么? ajax是一种在无需重新加载整个网页的情况下,能够更新部分网页的技术. ajax是一种用于创建的快速动态网页的技术. 当async:true时,表示异步执行ajax代码:当as ...
- C#实现DirectShow技术开发准备
DirectShow组件在“C:\WINDOWS\system32”目录下的Quartz.dll动态库中,要使C#代码引用COM对象和接口,必须将COM类型库转换为.NET框架元数据,从而有效地创建一 ...
- [转载] Hive与HBase的联系与区别
转载自http://blog.csdn.net/wangmuming/article/details/23954527和http://www.cnblogs.com/justinzhang/p/427 ...
- 获取IP-linux(经典-实用)
Linux系统获取网卡ip 1.正宗的有6种取ip的方法 sed(3) +awk(2)+egrep(1) sed(替换): ( )\1 [^0-9.] 掐头|去尾 awk(分隔符): ...
- AI 新技术革命将如何重塑就业和全球化格局?深度解读 UN 报告(上篇)
欢迎大家前往腾讯云社区,获取更多腾讯海量技术实践干货哦~ 张钦坤 腾讯研究院秘书长蔡雄山 腾讯研究院法律研究中心副主任祝林华 腾讯研究院法律研究中心助理研究员曹建峰 腾讯研究院法律研究中心高级研究员 ...
- JAVA基础面试(二)
11.是否可以从一个static方法内部发出对非static方法的调用? 不可以.因为非static方法是要与对象关联在一起的,必须创建一个对象后,才可以在该对象上进行方法调用,而static方法调用 ...
- 33.Linux-实现U盘自动挂载(详解)
1.当我们每次插入u盘后,都会自动创键U盘的设备节点/dev/sda%d 这是因为里面调用了device_create()实现的, busybox的mdev机制就会根据主次设备号等信息,在/dev下创 ...