1.url路径参数获取
-
目标:知道如何获取路径参数
-
获取的url参数,获取的是最后一个/-? 之间的字符串
-
转换器语法: <>
@app.route('/<user_id>') def index(user_id): return 'user_id is %s' %(user_id)
2.系统常用的转换器
-
d11_system_converter.py
-
string 字符串
@app.route('/<string:param>') def index(param): return 'param is %s' %(param)
-
int 整形转换器
@app.route('/<int:param>') def index(param): return 'param is %s' %(param)
-
float 转换器
@app.route('/<float:param>') def index(param): return 'param is %s' %(param)
-
小结
1.过滤不符合规则的请求路由 2.转换数据类型 3.获取参数
3.自定义转换器
-
目标:知道如何自定义转换器
-
原因:但系统的转换器不能满足我们的需求,就需要自定义转换器。
-
比如: 手机号码
-
步骤: d12_custom_converter.py
-
1.自定义自己的转换器类
class Mobile(BaseConverter): # 匹配手机号码 regex = r'1[3-9]\d{9}'
-
2.把自定义的转换器类添加到系统的转换器列表中
app.url_map.converters['mobile'] = Mobile
-
3.直接使用自定义转化器
@app.route('/<mobile:param>') def index(param): return 'param is %s' %(param)
-
4.使用request对象获取参数
-
目标:可以使用request对象获取除了url参数以外的其他参数
-
d13_request.py
导入: from flask import request args : 获取查询字符串的参数 form : 获取表单的参数 data : 获取请求体的原始数据 files: 获取文件 headers : 获取请求头数据 url : 获取请求的完整路径 method : 获取请求的方法 cookies : 获取cookie数据
-
小结
所有的业务基本都是从获取参数开始,所以一定要熟练使用request对象
5.返回响应
-
目标:知道在flask中返回响应
-
1.直接返回字符串
@app.route('/') def index(): # 1.直接返回字符串 return 'index'
-
2.返回响应对象
# 构建响应对象 response = make_response('index') return response
-
小结:
1.直接返回字符串,本质就是返回响应对象。只不过创建响应对象由flask帮我们完整
6.返回自定义状态码和头部
-
目标:知道如何设置响应头和状态码
-
d15_status_headers.py
-
1.以元组的方式返回
return 'index', 666, {'name': 'laowang'}
-
2.使用响应对象设置
response = make_response('index') response.status_code = 555 response.headers['age'] = 18 return response
7.返回json数据
-
目标:知道如何在flask中返回json数据
-
d16_json.py
-
1.使用json模块
data = { 'name': '老王', 'age': 18 } # 1.使用json模块 return json.dumps(data, ensure_ascii=False), 200, {'Content-Type': 'application/json'}
-
2.使用flask提供的jsonify函数
# 2.1 字典参数 # return jsonify(data) # 2.2 使用关键字参数 return jsonify(name='laowang', age=19)
8.返回jinja2模板
-
目标:知道在flask中使用render_template渲染模板
-
d17_template.py
-
使用步骤
@app.route('/') def index(): return render_template('user/d17_template.html', name='laowang', age=18)
9.重定向
-
目标:知道在flask中使用redirect进行重定向
-
d18_redirect.py
-
1.重定向到内部资源
return redirect('/index2')
-
2.重定向到外部资源
return redirect('https://www.baidu.com')
10.cookie使用
-
目标:可以使用response设置cookie, 使用request对象获取cookie
-
作用: 状态保持
-
文件: d01_cookie.py
-
设置cookie
response = make_response('index') # response.set_cookie(key, value, max_age) max_age: cookie的有效期时间 response.set_cookie('name', 'laowang', max_age=10) return response
-
获取cookie
name = request.cookies.get('name')
-
删除cookie
response.delete_cookie('name')
-
原理
设置响应头Set-Cookie,浏览器就会自动保存cookie 删除原理:设置一个过期的时间
11.session使用
-
目标:知道使用session对象设置和获取sessoin数据
-
文件: d02_session.py
-
无论是设置session, 还是获取session数据,都需要设置secret_key
-
设置secret_key
# app.secret_key = '' app.config['SECRET_KEY'] = ''
-
设置session数据
# 设置session @app.route('/set_session') def set_session(): value = request.args.get('name') # 设置session数据 session['name'] = value return 'index'
-
获取session数据
# 获取session @app.route('/get_session') def get_session(): # 获取session数据 name = session.get('name') return 'the name is %s' %(name)
-
小结
在Flask中,session的数据是存放在浏览器中。称为浏览器session。 这样做的好处:减轻服务器的压力