from flask import Flask, request,render_template,jsonify,make_response,redirect
from flask_script import Manager
1.# 获取flask对象
from utils.functions import login_required
app = Flask(__name__)
2.# 使用router绑定路由和执行的视图函数的关联关系
@app.route('/hello/')
def hello():
return 'hello world'
3.定义参数规则# 路由规则,定义: <转换器:参数名>
# django中路由定义: int参数: path('/uid/<int:id>/', xxxx)
# 字符串参数:path('/uid/<name>/', xxxx)
# 字符串参数:path('/uid/<str:name>/', xxxx)
# uuid参数:path('/uid/<uuid:uid>/', xxxx)
# path参数:path('/uid/<path:uid>/', xxxx)
# 正则表达式 Django2.0以上 re_path('/uid/(\d+)/')
# 正则表达式 Django2.0以下 url('/uid/(\d+)/')
# url('/uid/(?P<id>\d+)/')
# flask中路由定义: int参数: route('/uid/<int:id>/')
# 字符串: route(‘/uid/<name>/’)
# 字符串: route(‘/uid/<string:name>/’)
# uuid: route(‘/uid/<uuid:uid>/’)
# float: route(‘/uid/<float:uid>/’)
# path路径: route(‘/uid/<path:uid>/’)
@app.route('/uid/<int:id>/')
def get_id(id):
return 'id: {}'.format(id)
@app.route('/uname/<name>/')
def get_name(name):
print(type(name))
return 'name:{uname}'.format(uname=name)
@app.route('/uname2/<string:name>/')
def get_name2(name):
return 'name2:{}'.format(name)
@app.route('/uuid/<uuid:uid>/')
def get_uid(uid):
return 'uid:%s'% uid
@app.route('/price/<float:p>/')
def get_p(p):
return 'price:{}'.format(p)
@app.route('/path/<path:pth>/')
def get_pth(pth):
return 'path:{}'.format(pth)
4. request请求方法
# @app.route('/res/',methods=['GET','POST','PUT'])
# def res():
# if request.method == 'GET':
# return 'GET请求响应'
# if request.method == 'POST':
# return 'POST请求'
@app.route('/res/',methods=['GET','POST','PUT'])
def res():
if request.method == 'GET':
# django中GET取值: request.GET.get() request.GET.getlist()
# flask中GET取值: request.args.get(key) request.args.getlist(key)
names = request.args.getlist('name')
age = request.args.get('age')
return 'GET'
if request.method == 'POST':
# django中POST取值: request.POST.get() / getlist()
# flask中POST取值: request.form.get() / getlist()
names = request.form.getlist('name')
age = request.form.get('age')
return 'POST'
# 上传文件
request.files
# 获取cookie
request.cookies
# 方式
request.method
# 路由
request.path
if request.method == 'PUT':
# put、patch、delete取值方式和post一样
# request.form.get() 、 getlist()
return 'PUT请求响应'
@app.route('/req/', methods=['GET', 'POST'])
def req():
if request.method == 'GET':
# 响应字符串
# return 'GET请求响应'
# 响应页面
# return render_template('index.html')
# 响应json数据
# return jsonify({'code': 200, 'msg': '请求成功'})
# 响应中绑定cookie参数
# 在Django中: HttpResponse()/redirect()/HttpResponseRedirect()
# 在Flask中: make_response()
# response = make_response(render_template('index.html'), 200)
# response.set_cookie('token', '124567890', max_age=3000)
# response.delete_cookie('token')
# return response
# 重定向(跳转)
return redirect('/hello/')
5.渲染页面
@app.route('/login/',methods=['GET','POST'])
def login():
if request.method == 'GET':
return render_template('login.html')
if request.method == 'POST':
username = request.form.get('username')
password = request.form.get('password')
if username == 'aa' and password == '123456':
# 登陆成功,跳转到首页
# redirect和render_template区别
res = redirect('/index/')
res.set_cookie('token','123456',max_age=3000)
return res
return render_template('login.html')
@app.route('/index/')
@login_required
def index():
return render_template('index.html')
if __name__ == '__main__':
# 启动
# 解析命令行中传递的参数
# print(sys.argv)
# host = sys.argv[1]
# port = sys.argv[2]
# app.run(host=host, port=port, debug=True)
# 使用Manager管理flask对象
manage = Manager(app)
# 启动命令行: python hello.py runserver -h -p -d
manage.run()
6.装饰器
# 1.外层函数嵌套内层函数
# 2.外层函数返回内层函数
# 3.内层函数使用外层函数传递的参数
# 装饰器 两层函数,外层调用内层函数
# 内测函数实现功能后调用原函数 返回原函数结果
from functools import wraps
from flask import request
from werkzeug.utils import redirect
def login_required(func):
# func是被装饰的函数
@wraps(func)
def check():
token = request.cookies.get('token')
if not token:
return redirect('/login/')
if token != '123456':
return redirect('/login/')
return func()
return check