学习目标
URL与视图
1、URL与函数的映射 2、构造URL 3、指定URL末尾的斜杠
4、指定HTTP方法 5、页面跳转和重定向
关于响应
一、URL与函数的映射
@装饰函数,路由就是url(统一资源定位符),路由映射到函数上,调用路由就是调用函数的运行结果
- 不携带参数
/list/
- 不携带参数且以/作为结尾
/list/
若用户没有添加结尾/,浏览器会自动添加 - 携带参数
/list/<sid>
- 给参数加上指定类型的限制 ,如
/list/<int:sid>
- path可以携带/的参数,如
/list/<path:sid>'
- any类型 指定多种类型
# 视图函数
@app.route('/')
def hello_python():
return 'hello python'
@app.route('/list/<sid>')
def article(sid):
return '这是第{}篇文章'.format(sid)
# 给参数加上指定类型的限制
# 1、int类型或float类型
@app.route('/list/<int:sid>')
def article(sid):
return '这是第{}篇文章'.format(sid)
# 2、string类型中 / 是作为参数的分隔符 ,不能接收/的字符串
@app.route('/list/<string:sid>')
def article(sid):
return '这是第{}篇文章'.format(sid)
# 注意:若是自己在末尾有写到/,那么以/作为严格的结尾,那么网页中若自己没写,可以自动补全
@app.route('/list/<int:sid>/')
def article(sid