从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. python 进程池(multiprocessing.Pool)和线程池(threadpool.ThreadPool)的区别与实例

    一般我们是通过动态创建子进程(或子线程)来实现并发服务器的,但是会存在这样一些缺点: 1.动态创建进程(或线程)比较耗费时间,这将导致较慢的服务器响应.  2.动态创建的子进程通常只用来为一个客户服务 ...

  2. 设备类型检测大全---userAgent

    对各种类型的设备的检测,以及所使用的浏览器的类型 function detect(ua) { var os = this.os = {}; var browser = this.browser = { ...

  3. Java多线程Master-Worker模式

    Java多线程Master-Worker模式,多适用于需要大量重复工作的场景中. 例如:使用Master-Worker计算0到100所有数字的立方的和 1.Master接收到100个任务,每个任务需要 ...

  4. 谈一次java web系统的重构思路

    ——略谈Java web软件如何提供二次开发接口 接手公司的一个Java web软件产品,该软件采用传统的dwr框架.dwr框架相当于一个中间层,使得javascript能够识别Java类对象,进而能 ...

  5. MYSQL触发器在PHP项目中用来做信息备份、恢复和清空

    案例:通过PHP后台代码可以将员工的信息删除,将删除的员工信息进行恢复(类似于从回收站中恢复员工信息),并且还可以将已经删除的员工进行清空(类似于清空回复站的功能). 思路:要有一张员工表,还要有一张 ...

  6. PLSQL锁表之后改如何操作

    (1)查看哪个表被锁select b.owner,b.object_name,a.session_id,a.locked_mode from v$locked_object a,dba_objects ...

  7. centos7.2构建Python3.5开发环境

    1.本次使用的是一台全新的腾讯云主机,首先获取linux系统版本信息. [root@VM_46_121_centos ~]# cat /etc/redhat-release <本系统默认自带py ...

  8. mysql存储过程--学习

    -- 存储过程示例一   inDROP DATABASE IF EXISTS tdemo;CREATE DATABASE tdemo CHARACTER SET=utf8; USE tdemo;CRE ...

  9. 一款很便捷很实用的框架——vue.js

    Hello,大家好!今天给大家带来一款十分好用的框架--vue.js! Vue.js是一套构建用户界面的渐进式框架.它 只关注视图层, 采用自底向上增量开发的设计. Vue 的目标是通过尽可能简单的 ...

  10. python进阶------进程线程(一)

    Python中的进程线程 一.进程线程的概念 1.1进程: 进程就是一个程序在一个数据集上的一次动态执行过程.进程一般由程序.数据集.进程控制块三部分组成.我们编写的程序用来描述进程要完成哪些功能以及 ...