从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使用遇到的各种问题及解决方法的更多相关文章

  1. ##Django中Application labels aren't unique解决方法##

    pip更新了所有插件,发现了按平常编码遇到些问题,记录下. Django错误 django.core.exceptions.ImproperlyConfigured: Application labe ...

  2. Django与Vue语法冲突问题完美解决方法

    当我们在django web框架中,使用vue的时候,会遇到语法冲突. 因为vue使用{{}},而django也使用{{}},因此会冲突. 解决办法1: 在django1.5以后,加入了标签: {% ...

  3. django 渲染模板与 vue 的 {{ }} 冲突解决方法

    如果不可避免的在同一个页面里既有 django 渲染又有 vue 渲染的部分,可有 2 种方式解决 方法一: 采用 vue 的 delimiters  分隔符. new Vue({ delimiter ...

  4. Django 1.10 找不到静态资源解决方法

    测试版本:Django 1.10 问题:Django项目找不到静态资源 解决方法: 1.首先你需要在自己的app下面创建2个目录 static 和  templates 树形结构如下(DjangoPr ...

  5. Django的POST请求时因为开启防止csrf,报403错误,及四种解决方法

    Django默认开启防止csrf(跨站点请求伪造)攻击,在post请求时,没有上传 csrf字段,导致校验失败,报403错误 解决方法1: 注释掉此段代码,即可. 缺点:导致Django项目完全无法防 ...

  6. Django忘记管理员账号和密码的解决办法

    看着Django的教程学习搭建网站,结果忘记第一次创建的账号和密码了.结果搭建成功以后,一直无法登陆到管理页面,进行不下去了. 如图所示: 在网上找了很多的方法都不行,最后使用新建一个superuse ...

  7. 使用django建博客时遇到的URLcon相关错误以及解决方法。错误提示:类型错误:include0获得一个意外的关键参数app_name

    root@nanlyvm:/home/mydj/mysite# python manage.py runserver Performing system checks... Unhandled exc ...

  8. 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 ( ...

  9. django框架使用mysql报错,及两种解决方法

    1.django框架 settings.py文件中部分代码: DATABASES = { # 'default': { # 'ENGINE': 'django.db.backends.sqlite3' ...

随机推荐

  1. C++获取Windows7 32位系统中所有进程名(类似于任务管理器中的进程)

    代码是网上查找资料,然后自己调试,修改之后可以运行. 系统:win7 32位,VS2008 ------------------------------------------------------ ...

  2. 计蒜之道 初赛第一场B 阿里天池的新任务(简单)

    阿里“天池”竞赛平台近日推出了一个新的挑战任务:对于给定的一串 DNA 碱基序列 tt,判断它在另一个根据规则生成的 DNA 碱基序列 ss 中出现了多少次. 首先,定义一个序列 ww: \displ ...

  3. 蓝桥杯-算法训练--ALGO-5 最短路

    问题描述 给定一个n个顶点,m条边的有向图(其中某些边权可能为负,但保证没有负环).请你计算从1号点到其他点的最短路(顶点从1到n编号). 输入格式 第一行两个整数n, m. 接下来的m行,每行有三个 ...

  4. layui数据表格以及传数据方式

    数据表格一: <div style="margin:0px; background-color: white; margin:0 10px;"> <blockqu ...

  5. 在为知笔记中使用Markdown和思维导图

    为知笔记Wiz是一款很好的网摘和笔记工具,作为为知的忠实用户,我在为知收费后第一时间就购买了两年的授权,毕竟这么多年积累的资料都在为知上,我也习惯了使用Wiz来做些工作相关的笔记.为知笔记自带Mark ...

  6. 二叉搜索树 (BST) 的创建以及遍历

    二叉搜索树(Binary Search Tree) : 属于二叉树,其中每个节点都含有一个可以比较的键(如需要可以在键上关联值), 且每个节点的键都大于其左子树中的任意节点而小于右子树的任意节点的键. ...

  7. 二:熟悉 TCP/IP 协议

    一篇文章带你熟悉 TCP/IP 协议(网络协议篇二) 同样的,本文篇幅也比较长,先来一张思维导图,带大家过一遍. 一图看完本文 一. 计算机网络体系结构分层 计算机网络体系结构分层计算机网络体系结构分 ...

  8. Vue.js优雅的实现列表清单

        一.Vue.js简要说明 Vue.js (读音 /vjuː/) 是一套构建用户界面的渐进式框架.与前端框架Angular一样, Vue.js在设计上采用MVVM模式,当View视图层发生变化时 ...

  9. 64位win7下安装Boost 1.59.0 + boost.python 1.59.0 + gccxml + pygccxml + pyplusplus(py++)

    由于安装过程中实在是出现了N多问题,所以不得不专门写个帖子来记录一下这破东西在Win7下的安装过程,避免以后还要再用的时候踩坑. 1.Boost简介 Boost库是一个可移植.提供源代码的C++库,作 ...

  10. windows下配置php + mysql环境

    一.php服务器环境:apache + php安装与配置 1.下载apache.(httpd.apache.com) apache官网只提供源码,编译文件会有跳转到别的网站的下载地址. 如需VC9或V ...