1.安装视频电影模块
1.mkvirtualenv -p python3 movie
2.workon
3.workon movie
4.pip install flask_redis
2.session的设置删除和使用
from flask import Flask,session
app=Flask(__name__)
app.secret_key='afsdsafjsdfhajdsafsa6666'
app.debug=True
@app.route('/',methods=['GET','POST'])
def index():
session['name']='sxq'
return 'hello'
@app.route('/delete', methods=['GET', 'POST'])
def delete():
session.pop('name')
return '删除了'
@app.route('/order', methods=['GET', 'POST'])
def order():
print(session['name'])
return 'order'
if __name__ == '__main__':
print(app.config)
app.run()
3.闪现(message)放在了session里
from flask import Flask, request, make_response, session, flash, get_flashed_messages
app=Flask(__name__)
app.secret_key='fsaffdafsfadfaf'
app.debug=True
@app.route('/index', methods=['GET', 'POST'])
def index():
flash('sxq-nb')
return 'hello'
@app.route('/order', methods=['GET', 'POST'])
def order():
res=get_flashed_messages()
print(res)
return '6666'
if __name__ == '__main__':
print(app.config)
app.run()
-设置:flash('aaa')
-取值:get_flashed_message()
-
-假设在a页面操作出错,跳转到b页面,在b页面显示a页面的错误信息
from flask import Flask,flash,get_flashed_messages,request,redirect
app = Flask(__name__)
app.secret_key = 'asdfasdf'
@app.route('/index')
def index():
val = request.args.get('v')
if val == 'oldboy':
return 'Hello World!'
flash('超时错误',category="x1")
return "ssdsdsdfsd"
@app.route('/error')
def error():
"""
展示错误信息
:return:
如果get_flashed_messages(with_category=True)
"""
data = get_flashed_messages(category_filter=['x1'])
if data:
msg = data[0]
else:
msg = "..."
return "错误信息:%s" %(msg,)
if __name__ == '__main__':
app.run()
from flask import Flask, request, make_response, session, flash, get_flashed_messages
app=Flask(__name__)
app.secret_key='fsaffdafsfadfaf'
app.debug=True
@app.route('/index', methods=['GET', 'POST'])
def index():
flash('sxqNB',category='error1')
return 'hello'
@app.route('/order', methods=['GET', 'POST'])
def order():
res = get_flashed_messages(category_filter=['error'])
print(res)
return '6666'
@app.route('/order2', methods=['GET', 'POST'])
def order2():
res = get_flashed_messages(category_filter=['error1'])
print(res)
return 'sxq6666'
if __name__ == '__main__':
print(app.config)
app.run()
4.session的使用
from flask import Flask,session
app = Flask(__name__)
app.secret_key = 'fsaffdafsfadfaf'
app.debug = True
@app.before_request
def defore(*args,**kwargs):
print(args)
print(kwargs)
print('请求来了执行我')
@app.route('/', methods=['GET', 'POST'])
def index():
return 'hello'
if __name__ == '__main__':
print(app.config)
app.run()
5.请求扩展
from flask import Flask,session,request,redirect
app = Flask(__name__)
app.secret_key = 'fsaffdafsfadfaf'
app.debug = True
@app.before_request
def before(*args,**kwargs):
if request.path=='/login':
return None
else:
name=session.get('user')
if not name:
return redirect('/login')
else:
return None
@app.before_request
def before2(*args,**kwargs):
print('before222222222')
@app.after_request
def after(response):
print('我走了')
return response
@app.route('/login', methods=['GET', 'POST'])
def login():
session['user']='sxq'
return 'hello'
@app.route('/order', methods=['GET'])
def order():
print('我是order')
return '我是order页面'
if __name__ == '__main__':
app.run()
- 若有多个before从上向下的打印顺序
- 若有多个after从下向上的打印顺序
6.错误页面处理,绑定错误的状态码
@app.errorhandler(500)
def error_500(arg):
return render_template('error.html',message='505错误')
@app.errorhandler(404)
def error_404(arg):
return render_template('error.html',message='404错误')
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Error</title>
</head>
<body>
{{message}}
</body>
</html>
7.全局标签
@app.template_global()
def sb(a1, a2):
return a1 + a2
8.全局过滤器
@app.template_filter()
def db(a1, a2, a3):
return a1 + a2 + a3