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' ...
随机推荐
- 无阻赛的脚本(js脚本延迟方法)
js脚本的加载与执行 1.延迟脚本(defer属性) 带有defer属性的script标签,可以放置在文档的任何位置,在页面解析到该标签时,会开始下载该脚本,但是不会立即执行,直到dom加载完成(on ...
- js数组遍历some,foreach,map,filter,every对比
1. [...].some(ck)函数 ---- 某个一个为true,则为true 对数组中每个元素执行一次ck函数,知道某个元素返回true,则直接返回true.如果都返回f ...
- hook in PostgreSQL初探
HOOK IN POSTGRESQL 初探 前言 众所周知,PostgreSQL具有很好的扩展性,是一个可以"开发"的数据库.在PostgreSQL里面,你可以定制你自己的Type ...
- 腾讯Java程序员第二轮面试11个问题,你会几个?
此前,分享了阿里巴巴.网易.百度等多家名企的JAVA面试题. 这也引来了不少程序员网友们的围观. 其中,也有相当一部分网友是已经从事Java开发好多年的程序员,当他们阅读完JAVA面试题的反应是:一个 ...
- Robotframework自动化系统:筛选结果数量统计
Robotframework自动化系统:筛选结果数量统计 上一个节点已经可以随机选中某一个下拉框的值,我们在使用evaluate随机数的时候需要计算下拉选项总数,这时候我们是手工计算输入的:这时候如果 ...
- linux 安装 Elasticsearch5.6.x 详细步骤以及问题解决方案
在网上有很多那种ES步骤和问题的解决 方案的,不过没有一个详细的整合,和问题的梳理:我就想着闲暇之余,来记录一下自己安装的过程以及碰到的问题和心得:有什么不对的和问题希望及时拍砖. 第一步:环境 li ...
- Grafana+Prometheus系统监控之钉钉报警功能
介绍 钉钉,阿里巴巴出品,专为中国企业打造的免费智能移动办公平台,含PC版,Web版和手机版.智能办公电话,消息已读未读,DING消息任务管理,让沟通更高效:移动办公考勤,签到,审批,企业邮箱,企业网 ...
- PostgreSQL 下生成 UUID(Guid)
最近在Windows 10 下安装了 PostgreSQL(postgresql-9.6.3-1-windows.exe),在学习过程中,发现PostgreSQL 支持UUID(Guid)类型,但是却 ...
- struts2 内容记录
多xml文件配置 在开发过程中我们经常会将每一张表(如:user表)的struts.xml文件分开,便于管理,故需要建立struts_user.xml文件管理请求等.那么需要用到inculde标签. ...
- 【Spring】Spring MVC高级技术
前言 前面学习了简单的Spring Web知识,接着学习更高阶的Web技术. 高级技术 Spring MVC配置的替换方案 自定义DispatcherServlet配置 在第五章我们曾编写过如下代码. ...