首页
登录
搜索
登录
搜索
emer
累计撰写
197
篇文章
累计收到
496
条评论
首页
栏目
首页
登录
自定义幻灯片
最新文章
2019-11-5
Django自定义中间件Middleware
from django.utils.deprecation import MiddlewareMixin # from django.http import HttpResponse from django.shortcuts import HttpResponse, redirect # 方式一: class MyMiddleware(MiddlewareMixin): def process_request(self, request): next_url = request.path_info if not request.path_info.startswith("/login/"): # 做登录验证 login_flag = request.session.get("login", "") if not login_flag: return redirect("/login/?next={}".format(next_url)) def process_view(self, request, view_func, view_args, view_kwargs): pass def process_exception(self, request, exception): if isinstance(exception, ValueError): return HttpResponse("404") def process_response(self, request, response): return response # 这里必须返回response # 方式二: class SimpleMiddleware(object): def __init__(self, get_response): self.get_response = get_response # 一次性配置和初始化。 def __call__(self, request): # Code to be executed for each request before # the view (and later middleware) are called. # 这里写的代码会在视图被调用前执行来处理请求 response = self.get_response(request) # 这里写的代码会在视图调用后执行来处理响应 # Code to be executed for each request/response after # the view is called. return response def process_view(self, request, view_func, view_args, view_kwargs): pass def process_exception(self, request, exception): pass def process_template_response(self, request, response): pass # 在settings.py里的下面列表中添加自定义的中间件来激活该中间件 MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware', 'mymiddlewares.middlewares.MyMiddleware', 'mymiddlewares.middlewares.SimpleMiddleware', ]
2019年-11月-5日
48 阅读
0 评论
未分类
2019-11-4
JavaScript声明一个Class
在ES5与ES6中class其实背后的原理是一样的 我们先看Es5是如何定义一个class的 // 首先我们先定义一个动物类 let Animal = function (type) { this.type = type } // 因为有些方法是动物们共有的属性那么我们定义一个原型方法 Animal.prototype.eat = function () { console.log('吃') } // 实例化对象 let dog = Animal('dog') let monkey = Animal('monkey') // 打印 console.log(dog) console.log(monkey) // Animal {type: "dog"} // lesson2.js:15 Animal {type: "monkey"} // 如果我们需要修改原型链上面的方法呢? // 我们可以随便找一个实例 monkey.constructor.prototype.eat = function() { console.log('不吃') } 下面我们在ES6中声明一个class class Animal{ constructor(type) { this.type = type } eat() { console.log('吃') } } // 实例化 let dog = new Animal('dog') let mokey = new Animal('monkey')
2019年-11月-4日
39 阅读
0 评论
未分类
2019-11-4
JavaScript数组的查找
我们在ES5中常用的方法filter 我们来看一下 let arrary = Array.of(1,2,3,4,5) let find = array.filter(x => { return x === 3; }) console.log(find) // 运行程序 // [3] 使用filter时候的弊端 filter不管有没有查找到需要的元素都会将数组遍历完成 而ES6中新加入了find只需要返回true将不会继续遍历相当于break 下面我们来看一下 let array = Array.of(1,2,3,4,5) let find = array.find(x => { return x === 2 }) console.log(find) // 好的运行程序。我们会发现输出2,这个返回值是数组的值而不是index find的弊端在于没有返回索引 这是又有一个findIndex解决了这个问题 let array = Array.of(1,2,3,4,5) let find = array.findIndex(x => { return x===3 }) console.log(find)
2019年-11月-4日
50 阅读
0 评论
未分类
2019-11-4
JavaScript中生成数组的几种方式
第一种最简单的 new Array(5) // 生成5个 empty 的数组元素 new Array(1,2,3,4,5) // 生成5个值为1,2,3,4,5的元素 第二种使用伪数组概念 Array.from({ length: 100 }, x => 1) // 生成100个元素值为1的数组 第三种使用fill Array(100).fill(5) // 生成100个元素为5的数组 Array(100).fill(5,2,10) // 生成100个元素其中索引为2的数组~索引为9的数组值为5 其余均为empty 第四种生成一个数组 Array.of(2) // 生成一个值为2的数组 Array.of(1,2,3,4,5) // 生成五个值为1~5的数组
2019年-11月-4日
45 阅读
0 评论
未分类
2019-11-4
JavaScript中的伪数组转换
在进行转换之前首先我们要明白什么是伪数组。 伪数组就是指一个类似于数组的集合。同样它具有length以及index数字索引 好了我们先来定义一个伪数组 let pseudoArray = { 0: 'hello', 1: 'world', length: 2 } 好了一个经典的伪数组就定义好了 这时候我们使用forEach遍历一下 pesudoArray.forEach(x => { console.log(x) }) // 这个时候运行程序浏览器会报错 // lesson2.js:6 Uncaught TypeError: pseudoArray.forEach is not a function at lesson2.js:6 因为它看似数组其实不然 我们这个时候需要转换一下 怎么转换呢? Es5之中我们会使用数组对象上面的一个方法 let array = [].slice.call(pseudoArray) array.forEach(x => { console.log(x) }) // 运行程序这个时候会正确的输出 // hello // world 有的同学可能会说很麻烦啊!有没有更简单的方法呢? 在ES6中array提供了from方法可以直接转化 let array = Array.from(pseudoArray) array.forEach(x => { console.log(x) }) // 这个时候程序也可以正常的运行 // hello // world // 和之前ES5中繁琐的步骤是一样的
2019年-11月-4日
41 阅读
0 评论
未分类
2019-11-4
ES6数组遍历复习
1、for(let i = 0; i < array.length; i++) {}方式 // 声明一个数组 array = [1,2,3,4,5] // 遍历 for(let i = 0; i < array.length; i++) { console.log(i,array[i]) } // 依次输出 // 0, 1 // 1,2 // 2,3 // 3,4 // 4,5 2、forEach遍历方式 // 这里直接使用上面的数组了 array.forEach( function (x) { console.log(x) }) // 输出1,2,3,4,5 // 需要值得注意的是forEach不支持break以及continue 3、every遍历方式 使用forEach方式遍历的场景是需要遍历每一个元素,因为forEach不能在执行过程中停止掉 而使用every方式遍历的大部分场景都是用于判定一个元素是否存在这个数组中的时候使用every 下面请看example let array = [1,2,3,4,5] array.every( x => { console.log(x) return true // 注意这里如果返回true时将继续遍历 }) // 程序结束输出 // 1 // 2 // 3 // 4 // 5 // 下一个example array.every( x => { console.log(x) } ) // 程序结束输出 // 1 // 这里可能有很多同学疑惑我并没有返回false呀,为什么只遍历了一个呢? // 这里需要说明的是如果没有返回值将默认返回false 4、for...in... for...in...方法本不属于array,但是却可以用于遍历array。for...in...方法主要用于遍历对象 下面是一个example let array = [1,2,3,4,5] for ( i in array ) { console.log(i, array[i]) } // 这里输出的是 // 0,1 // 1,2 // 2,3 // 3,4 // 4,5 这里可能很多同学会问for in 本是给object设计的,那么array使用了就没有什么副作用吗? 我在这里的回答是有的,肯定是有的 for in 给我们带来便利的同时也会遍历到一些array上面的非原型对象 这里打个比方 我一开始new了一个array 后来我直接给这个array对象加上一个属性,同学们想一想这个属性他是可以遍历出来的吗? 我们来看一下 let array = [1,2,3,4,5] array.a = 'hello wrold' for ( i in array ) { console.log(i , array[i]) } // 运行结束,程序输出 // lesson2.js:4 1 2 // lesson2.js:4 2 3 // lesson2.js:4 3 4 // lesson2.js:4 4 5 // lesson2.js:4 a hello world 最后数组遍历的方法还有一个for...of... 可能我们大部分人的意识里面可遍历的数据类型只有object、array,但是到了es6之后我们就可以自定义数据结构,只要按照es6标准定义数据结构就可以遍历。但是这样的遍历不可以使用for、 for...in...只时候就要用到for...of...了
2019年-11月-4日
47 阅读
0 评论
未分类
2019-10-31
使用Django处理excel
感觉大部分没有使用到Django其实并不是标题所说的使用Django了。大部分还要是python原生内容为主 首先需要安装xlrd这个扩展包 pip3 install xlrd 然后在Django中接收到文件 import xlrd fs = reuqest.FILES.get('file_name'); fd = xlrd.open_workbook(filename=None, file_contents=fs.read()) table = fd.sheets()[0] rows = table.nrows for i in range(1, rows): ...
2019年-10月-31日
57 阅读
0 评论
未分类
2019-10-28
django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.2
使用python3.7+django2.2+pymysql时遇到这个错误, django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.2 别急,这主要是django2.2内部的一个版本限制在作怪处理方案 1.修复源码 按照文中配置,报错django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3. 原因:django2.2和pymysql版本不匹配。mysqldb不支持python3. 解决方案: 1、raise ImproperlyConfigured(‘mysqlclient 1.3.13 or newer is required; you have %s.’ % Database.version) django.core.exceptions.ImproperlyConfigured: mysqlclient 1.3.13 or newer is required; you have 0.9.3. 解决办法: C:\Python37\Lib\site-packages\django\db\backends\mysql(python安装目录)打开base.py,注释掉以下内容: if version < (1, 3, 13): raise ImproperlyConfigured(‘mysqlclient 1.3.13 or newer is required; you have %s.’ % Database.version) 2、File “C:\Python37\lib\site-packages\django\db\backends\mysql\operations.py”, line 146, in last_executed_query query = query.decode(errors=‘replace’) AttributeError: ‘str’ object has no attribute ‘decode’ 解决办法: 打开此文件把146行的decode修改为encode 注释后即可 ps.django2.2不支持pyMySQL,但2.2是LTS,有想过试试mysqldb,但是MySQLDB又不支持python3。。。。的确让人头大。
2019年-10月-28日
134 阅读
0 评论
未分类
2019-10-28
AttributeError: 'str' object has no attribute 'decode'
python3下列代码会报上边的错 print("Response:", resp.text.decode('unicode_escape')) 解决办法: print("Response:", resp.text.encode('utf-8').decode('unicode_escape')) 中间加上.encode('utf-8')即可
2019年-10月-28日
119 阅读
0 评论
未分类
2019-10-25
React生命周期函数
首先贴一张图片,直观深刻的理解一下 组件挂载前执行 1、componentWillMount 组件挂载后执行 2、componentDidMount 组件更新之前执行 3、shouldComponentUpdate 组件被更新之前执行但在shouldComponentUpdate之后执行,如果shouldComponentUpdate返回true执行 返回false不执行 4、componentWillUpdate 组件更新完成之后执行 5、componentDidUpdate 存在于自组件中,要从父组件接受参数,第一次存在于父组件中不会执行,父组件执行render会触发componentWillReceiveProps函数 6、compoentWillReceiveProps 当一个组件将被销毁之前执行 7、componentWillMount
2019年-10月-25日
99 阅读
0 评论
未分类
5
6
7
8
9