Flask学习笔记(二)
1.知识点
1.虚拟环境
1.1virtualenv
在系统级python环境下,安装virtualenv虚拟环境。我电脑安装了anconda,所以这一块我就不用操作了。-p参数指定使用系统级的哪一个python.exe 来执行。
win
pip install virtualenv
virtualenv abc#就会在当前地址下新建了一个环境文件夹abc
cd abc/Scripts#进入环境的scripts目录
activate#激活环境
linux
source abc/bin activate#linux是放在bin目录下的
1.2virtualenvwrapper
win
pip install virtualenvwrapper-win
mkvirtualenv abc#放在当前win用户的文件夹里面envs的文件夹下
workon abc#激活此环境
deactivate#推出此环境
rmvirtualenv abc#删除此环境
lsvirtualenv abc#列出环境
cdvirtualenv de#立即进入de环境
linux
pip install virtualenvwrapper
2.web与视图
1.url:scheme://host:port/path/?query-string=xxx#anchor
2.web服务器:负责处理http请求,响应静态文件,常见的有Apache,Nginx以及微软的IIS.
3.应用服务器:负责处理逻辑的服务器。比如php、python的代码,是不能直接通过nginx这种web服务器来处理的,只能通过应用服务器来处理,常见的应用服务器有uwsgi、tomcat等。
4.web应用框架:一般使用某种语言,封装了常用的web功能的框架就是web应用框架,flask、Django以及Java中的SSH(Structs2+Spring3+Hibernate3)框架都是web应用框架。
5.config设置配置文件的四种方式:
app.config['DEBUG'] = True#1.硬编码
app.config.update(DEBUG=True)#2.因为app.config是flask.config.Config的实例,而Config类是继承自dict,因此可以通过update方法
import config
app.config.from_object(config)#3.通过模块对象,将config.py文件的数据导入
app.config.from_pyfile('settings.py',silent=True)#4.也可使用.txt后缀的文件;默认是为False,找不到配置文件就会抛出异常
6.永久性重定向:http的状态码是301,多用于旧网址被废弃了要转到一个新的网址确保用户的访问,最经典的就是京东网站,你输入www.jingdong.com的时候,会被重定向到www.jd.com,因为jingdong.com这个网址已经被废弃了,被改成jd.com,所以这种情况下应该用永久重定向。
7.暂时性重定向:http的状态码是302,表示页面的暂时性跳转。比如访问一个需要权限的网址,如果当前用户没有登录,应该重定向到登录页面,这种情况下,应该用暂时性重定向。
8.redirect(url,code=301)#默认是302
3.jinjia2
1.在渲染模版的时候,默认会从项目根目录下的templates
目录下查找模版。也可以在Flask
初始化的时候指定template_folder
来指定模版的路径。
app=Flask(__name__,template_folder="xxx")
2.当需要渲染的参数比较多时,通过关键字传参方式,给html渲染传参。
@app.route("/key")
def key():
context={"a":1,"b":2,"c":3}
return render_template("key.html",**context)#==render_template("key.html","a"=1,"b"=2,"c"=3)
<html>
<head>key</head>
<title>key</title>
<body>
<h1>a={{a}}</h1>
<h1>b={{b}}</h1>
<h1>c={{c}}</h1>
</body>
</html>
3.html中,{{ … }}:用来装载变量,或者字典的key。
{% … %}:用来装载控制语句。
{# … #}:用来装载注释
4.过滤器:通过管道符号(|)进行使用,过滤器相当于是一个函数,把当前的变量传入到过滤器中,然后过滤器根据自己的功能,再返回相应的值,之后再将结果渲染到页面中。官网过滤器。
5.自定义过滤器(两种方式)
app.config['TEMPLATES_AUTO_RELOAD']=True
@app.template_filter('cut')
def cut(value):
value=value.replace("hello",'')
return value
def datetime_format(value,format="%Y年%m月%d日 %H:%M"):
return value.strftime(format)
app.add_template_filter(datetime_format,"dformat")#将自定义的datetime_format,以dformat为名添加到app的过滤器中
自定义时间过滤器
@app.route("/time_filter")
def time_filter():
now=datetime(2023,6,25,10,53,20)
return render_template("key.html",now=now)
@app.template_filter("handle_time")
def handle_time(time):
if isinstance(time,datetime):#首先判断是否时时间类型
now=datetime.now()
timestamp=(now-time).total_seconds()#时间间距
if timestamp<60:
return "刚刚"
elif timestamp>=60 and timestamp<60*60:
minutes=timestamp/60
return "%s分钟前"%int(minutes)
elif timestamp>=60*60 and timestamp<60*60*24:
hours=timestamp/(60*60)
return "%s小时前"%int(hours)
elif timestamp>=60*60*24 and timestamp<60*60*24*30:
days=timestamp/(60*60*24)
return "%s天前"%int(days)
else:
return time.strftime('%Y/%m/%d %H:%M')
else:
return time
<html>
<body>
<h1>now is :{{now|handle_time}}</h1>
</body>
</html>