flask框架路由常用定义方式总结
作者:笑-笑-生 时间:2021-04-18 22:12:29
本文实例讲述了flask框架路由常用定义方式。分享给大家供大家参考,具体如下:
路由的各种定义方式
请求方式限定
使用 methods 参数指定可接受的请求方式,可以是多种
@app.route('/',methods=['GET'])
def hello():
return '<h1>hello world</h1>'
路由查找方式
同一路由指向两个不同的函数,在匹配过程中,至上而下依次匹配
@app.route('/')
def hello():
return '<h1>hello world</h1>'
@app.route('/')
def hello_2017():
return '<h1>hello 2017</h1>'
所以上面路由 / 输出的结果为 hello 函数的结果
给路由传参示例
有时我们需要将同一类URL映射到同一个视图函数处理,比如:使用同一个视图函数 来显示不同用户的个人信息。
路由传递的参数默认当做string处理,这里指定int,尖括号中的内容是动态的,也可不指定类型
@app.route('/user/<int:id>')
def hello_itheima(id):
return 'hello itcast %d' %id
重定向redirect示例
from flask import redirect
@app.route('/')
def hello_itheima():
return redirect('http://www.itcast.cn')
返回JSON
from flask import Flask,json
@app.route('/json')
def do_json():
hello = {"name":"stranger", "say":"hello"}
return json.dumps(hello)
返回状态码示例
在 Python 中返回状态码有两种方式实现:
- 直接return
- 可以自定义返回状态码,可以实现不符合http协议的状态码,例如:error=666,errmsg='查询数据库异常',其作用是为了实现前后端数据交互的方便
- abort方法
- 只会抛出符合http协议的异常状态码,用于手动抛出异常
@app.route('/')
def hello_itheima():
return 'hello itcast',666
正则路由示例
在web开发中,可能会出现限制用户访问规则的场景,那么这个时候就需要用到正则匹配,限制访问,优化访问
导入转换器包
from werkzeug.routing import BaseConverter
自定义转换器并实现
# 自定义转换器
class Regex_url(BaseConverter):
def __init__(self,url_map,*args):
super(Regex_url,self).__init__(url_map)
self.regex = args[0]
app = Flask(__name__)
# 将自定义转换器类添加到转换器字典中
app.url_map.converters['re'] = Regex_url
@app.route('/user/<re("[a-z]{3}"):id>')
def hello_itheima(id):
return 'hello %s' %id
自带几种转换器
DEFAULT_CONVERTERS = {
'default': UnicodeConverter,
'string': UnicodeConverter,
'any': AnyConverter,
'path': PathConverter,
'int': IntegerConverter,
'float': FloatConverter,
'uuid': UUIDConverter,
}
希望本文所述对大家基于flask框架的Python程序设计有所帮助。
来源:https://blog.csdn.net/weixin_41790086/article/details/80540873
标签:flask,路由
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python读取指定字节长度的文本方法
2022-04-17 10:42:45
一文教你利用Python制作一个C盘清理器
2022-05-05 00:45:20
![](https://img.aspxhome.com/file/2023/9/81839_0s.png)
python random从集合中随机选择元素的方法
2023-07-05 08:29:45
windows安装python超详细图文教程
2023-08-01 15:19:32
![](https://img.aspxhome.com/file/2023/2/61762_0s.png)
js字符串日期yyyy-MM-dd转化为date示例代码
2023-08-06 16:35:58
python 动态迁移solr数据过程解析
2023-03-08 10:19:20
![](https://img.aspxhome.com/file/2023/6/68996_0s.png)
ThinkPHP中__initialize()和类的构造函数__construct()用法分析
2023-07-08 01:26:24
SEO与“nofollow”及“external nofollow”
2007-12-15 09:31:00
精巧支付宝导航条制作教程
2010-03-20 21:25:00
9个Web设计中常见的可用性错误[译]
2009-03-11 20:25:00
![](https://img.aspxhome.com/file/UploadPic/20093/11/01-10s.jpg)
优化Oracle停机时间及数据库恢复
2010-07-20 12:54:00
python利用beautifulSoup实现爬虫
2022-05-17 21:10:34
mysql limit查询优化分析
2023-11-16 00:51:04
PHP实现逐行删除文件右侧空格的方法 <font color=red>原创</font>
2023-11-22 05:11:25
详解tensorflow训练自己的数据集实现CNN图像分类
2023-02-28 10:47:04
![](https://img.aspxhome.com/file/2023/3/88993_0s.png)
CSS 表格元素内容的定位 0
2008-08-01 17:31:00
python进程间数据交互的几种实现方式
2022-11-05 09:32:12
FF和IE之间7个JavaScript的差异[译]
2009-05-04 18:19:00
在opera里css出现渲染问题
2009-01-15 12:19:00
python和node.js生成当前时间戳的示例
2022-05-26 05:07:43