查看路径参数类型
python
for converter_name, converter_class in app.url_map.converters.items():
print(f"{converter_name}: {converter_class}")
# 输出结果
default: <class 'werkzeug.routing.converters.UnicodeConverter'>
string: <class 'werkzeug.routing.converters.UnicodeConverter'>
any: <class 'werkzeug.routing.converters.AnyConverter'>
path: <class 'werkzeug.routing.converters.PathConverter'>
int: <class 'werkzeug.routing.converters.IntegerConverter'>
float: <class 'werkzeug.routing.converters.FloatConverter'>
uuid: <class 'werkzeug.routing.converters.UUIDConverter'>
一、string
python
@app.route('/user/<string:username>')
def show_string(username):
return f'type: string, value: {username}, python_type: {type(username).__name__}'
-
测试
/user/john_doe输出结果
type: string, value: john_doe, python_type: str
-
测试
/user/123输出结果
type: string, value: 123, python_type: str
-
测试
/user/john%20doe输出结果
type: string, value: john doe, python_type: str
二、int
python
@app.route('/article/<int:article_id>')
def show_int(article_id):
return f'type: int, value: {article_id}, python_type: {type(article_id).__name__}'
-
测试
/article/42输出结果
type: int, value: 42, python_type: int
-
测试
/article/0输出结果
type: int, value: 0, python_type: int
-
测试
/article/-100输出结果
Not Found
-
测试
/article/3.14输出结果
Not Found
-
测试
/article/abc输出结果
Not Found
-
测试
/article/10abc输出结果
Not Found
三、float
python
@app.route('/price/<float:price>')
def show_float(price):
return f'type: float, value: {price}, python_type: {type(price).__name__}'
-
测试
/price/19.99输出结果
type: float, value: 19.99, python_type: float
-
测试
/price/-5.5输出结果
Not Found
-
测试
/price/100输出结果
Not Found
-
测试
/price/100.0输出结果
type: float, value: 100.0, python_type: float
-
测试
/price/.5输出结果
Not Found
-
测试
/price/3.1415926输出结果
type: float, value: 3.1415926, python_type: float
-
测试
/price/10,5输出结果
Not Found
-
测试
/price/10k输出结果
Not Found
-
测试
/price/abc输出结果
Not Found
四、path
1、演示
python
@app.route('/path/<path:filepath>')
def show_path(filepath):
return f'type: path, value: {filepath}, python_type: {type(filepath).__name__}'
-
测试
/path/docs输出结果
type: path, value: docs, python_type: str
-
测试
/path/docs/api输出结果
type: path, value: docs/api, python_type: str
-
测试
/path/docs/api/v1输出结果
type: path, value: docs/api/v1, python_type: str
-
测试
/path/static/css/style.css输出结果
type: path, value: static/css/style.css, python_type: str
-
测试
/path/a/b/../d输出结果
type: path, value: a/b/../d, python_type: str
2、string 处理路径类型的问题
python
@app.route('/path/<string:filepath>')
def show_path(filepath):
return f'type: string, value: {filepath}, python_type: {type(filepath).__name__}'
-
测试
/path/docs输出结果
type: string, value: docs, python_type: str
-
测试
/path/docs/api输出结果
Not Found
-
测试
/path/docs/api/v1输出结果
Not Found
-
测试
/path/static/css/style.css输出结果
Not Found
-
测试
/path/a/b/../d输出结果
Not Found
五、uuid
python
@app.route('/resource/<uuid:resource_id>')
def show_uuid(resource_id):
return f'type: uuid, value: {resource_id}, python_type: {type(resource_id).__name__}'
-
测试
/resource/123e4567-e89b-12d3-a456-426614174000输出结果
type: uuid, value: 123e4567-e89b-12d3-a456-426614174000, python_type: UUID
-
测试
/resource/550e8400-e29b-41d4-a716-446655440000输出结果
type: uuid, value: 550e8400-e29b-41d4-a716-446655440000, python_type: UUID
-
测试
/resource/not-a-uuid输出结果
Not Found
-
测试
/resource/123输出结果
Not Found
-
测试
/resource/123e4567-e89b-12d3-a456-42661417400(短了)输出结果
Not Found
-
测试
/resource/123e4567-e89b-12d3-a456-4266141740000(长了)输出结果
Not Found