day03回顾
1.请求对象 - request
from flask import request
属性
1.request.method
2.request.args
3.request.form
4.request.cookies
5.request.headers
request.headers.get('referer')
1.获取 get 请求数据
使用超链接发送get请求,拼地址栏参数
<a href="/request?name=zsf&age=85">xxx</a>
name=request.args.get('name')
age =request.args.get('age')
使用js中的location对象,发送get请求并拼接参数
<script>
$btn.click(function(){
location.href='xxxxx?arg1=value1&arg2=value2'
});
</script>
2.获取 post 请求数据
request.form.get('xxx')
request.form.getlist('name_list')
2.响应对象
除了可以响应字符串和模板之外,还可以是响应对象或重定向
1.响应对象 - make_response()
from flask import make_response
resp = make_response('xxxx')
return resp
resp = make_response(render_template('xx.html',params=locals()))
return resp
2.重定向
由服务器通知浏览器向新的地址发送一个请求
from flask import redirect
resp = redirect('重定向地址')
return resp
3.文件上传
1.前端页面
1.form中的method的值必须为 post
2.form中的enctype的值必须为 multipart/form-data
大量数据上传的时候(如:超大文件),就不能使用网页上传了(主要是由于http协议不支持),需要使用单独的上传工具(C/S版的)
2.服务器端
使用 request.files 接收上传的文件
f = request.files['文件选择框名称']
f.save('static/'+f.filename)
os.path.dirname(__file__)
os.path.join(目录1,目录2,目录n)
1.模型 - Models
- 什么是模型
模型,是根据数据库中表的结构来创建出来的class,每一张表到编程语言中就是一个class,表中的每一个列,到编程语言中就是class中的一个属性. - 创建 和 使用模型 - ORM
- 什么是ORM
ORM:Object Relational Mapping
简称:ORM ,O/RM ,O/R Mappi
- 什么是ORM