")">
赞
踩
- import typing as t
-
- from flask import Flask
- from werkzeug.routing import BaseConverter
-
- app = Flask(__name__)
-
-
- class RegexConverter(BaseConverter):
- def __init__(self, url_map, items):
- super(RegexConverter, self).__init__(url_map)
- self.regex = items
-
- def to_python(self, value: str) -> t.Any:
- print("to python called")
- return value
-
-
- app.url_map.converters['re'] = RegexConverter
-
-
- @app.route('/<re("1234"):value>', methods=['GET', "POST"])
- def index(value):
- return f'<h1>hello{value} world</h1>'
-
-
- if __name__ == '__main__':
- app.run(debug=True)
如果访问
符合的正则才有数据,否则就没有数据
比如定义手机号正则就可以
- @app.route('/<re("1\d{10}"):value>', methods=['GET', "POST"])
- def index(value):
- return f'<h1>hello{value} world</h1>'
不符合规则就不行
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。