从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. A. 拼音魔法

    A. 拼音魔法 Time limit per test: 1.0 seconds Time limit all tests: 1.0 seconds Memory limit: 256 megabyt ...

  2. JavaScript中把Json字符串转化为对象

    1.采用eval()函数 定义和用法 eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码. 语法 eval(string) 参数 描述 string 必需.要计算的字符串, ...

  3. 行内脚本的位置放置----css阻塞行内脚本

    行内脚本:避免放置在css和其他资源之间. 若在head中,最好放在css样式表之前,如果放置在样式表之后,会引起css阻塞. css阻塞:由于浏览器要保证css和JavaScript的执行顺序,cs ...

  4. Golang源码探索(二) 协程的实现原理

    Golang最大的特色可以说是协程(goroutine)了, 协程让本来很复杂的异步编程变得简单, 让程序员不再需要面对回调地狱, 虽然现在引入了协程的语言越来越多, 但go中的协程仍然是实现的是最彻 ...

  5. JSTL中foreach与fn表达式

    在jstl中的fn标签也是我们在网页设计中经常要用到的很关键的标签,在使用的时候要先加上头 <%@ taglib uri=" http://java.sun.com/jsp/jstl/ ...

  6. Navi.Soft31.产品.登录器(永久免费)

    1系统简介 1.1功能简述 电商平台和传统店铺相比,确实方便不少,直接在网上下单,快递直接送货到家.这其中,做电商平台的童鞋表示压力很大,因为可能同时开很多店铺,每个店铺都要登录.查看订单量.发货拣货 ...

  7. C#删除区域实现透明

    最近在搞一个图形图像的项目.不知道经理为什么选择了C#语言,但还是要做,呵呵. 在期间出现一个比较难解决的问题如下: 删除当前图层的指定区域用来显示下面图层在这个区域的图像,相当于PS蒙版层的效果. ...

  8. Array类的Sort()方法

    刚复习了Array类的sort()方法, 这里列举几个常用的,和大家一起分享. Array类实现了数组中元素的冒泡排序.Sort()方法要求数组中的元素实现IComparable接口.如System. ...

  9. redis特性 存储 API 集群 等

    公司组内技术分享,刚好最近工作用redis构建缓存,所以想同事们分享关于redis的一些知识, 这些知识不仅仅是包括一些API层,而是一些关于redis功能功能特性的 目前为什么使用redis构建缓存 ...

  10. Cordic算法——verilog实现

    上两篇博文Cordic算法--圆周系统之旋转模式.Cordic算法--圆周系统之向量模式做了理论分析和实现,但是所用到的变量依然是浮点型,而cordic真正的用处是基于FPGA等只能处理定点的平台.只 ...