Python Flask 开发 - Flask 路径参数类型(string、int、float、path、uuid)

查看路径参数类型

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__}'
  1. 测试 /user/john_doe

    输出结果

    type: string, value: john_doe, python_type: str

  2. 测试 /user/123

    输出结果

    type: string, value: 123, python_type: str

  3. 测试 /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__}'
  1. 测试 /article/42

    输出结果

    type: int, value: 42, python_type: int

  2. 测试 /article/0

    输出结果

    type: int, value: 0, python_type: int

  3. 测试 /article/-100

    输出结果

    Not Found

  4. 测试 /article/3.14

    输出结果

    Not Found

  5. 测试 /article/abc

    输出结果

    Not Found

  6. 测试 /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__}'
  1. 测试 /price/19.99

    输出结果

    type: float, value: 19.99, python_type: float

  2. 测试 /price/-5.5

    输出结果

    Not Found

  3. 测试 /price/100

    输出结果

    Not Found

  4. 测试 /price/100.0

    输出结果

    type: float, value: 100.0, python_type: float

  5. 测试 /price/.5

    输出结果

    Not Found

  6. 测试 /price/3.1415926

    输出结果

    type: float, value: 3.1415926, python_type: float

  7. 测试 /price/10,5

    输出结果

    Not Found

  8. 测试 /price/10k

    输出结果

    Not Found

  9. 测试 /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__}'
  1. 测试 /path/docs

    输出结果

    type: path, value: docs, python_type: str

  2. 测试 /path/docs/api

    输出结果

    type: path, value: docs/api, python_type: str

  3. 测试 /path/docs/api/v1

    输出结果

    type: path, value: docs/api/v1, python_type: str

  4. 测试 /path/static/css/style.css

    输出结果

    type: path, value: static/css/style.css, python_type: str

  5. 测试 /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__}'
  1. 测试 /path/docs

    输出结果

    type: string, value: docs, python_type: str

  2. 测试 /path/docs/api

    输出结果

    Not Found

  3. 测试 /path/docs/api/v1

    输出结果

    Not Found

  4. 测试 /path/static/css/style.css

    输出结果

    Not Found

  5. 测试 /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__}'
  1. 测试 /resource/123e4567-e89b-12d3-a456-426614174000

    输出结果

    type: uuid, value: 123e4567-e89b-12d3-a456-426614174000, python_type: UUID

  2. 测试 /resource/550e8400-e29b-41d4-a716-446655440000

    输出结果

    type: uuid, value: 550e8400-e29b-41d4-a716-446655440000, python_type: UUID

  3. 测试 /resource/not-a-uuid

    输出结果

    Not Found

  4. 测试 /resource/123

    输出结果

    Not Found

  5. 测试 /resource/123e4567-e89b-12d3-a456-42661417400(短了)

    输出结果

    Not Found

  6. 测试 /resource/123e4567-e89b-12d3-a456-4266141740000(长了)

    输出结果

    Not Found

相关推荐
Hgfdsaqwr4 小时前
Django全栈开发入门:构建一个博客系统
jvm·数据库·python
guygg884 小时前
NOMA功率分配与64 QAM调制中的SIC的MATLAB仿真
开发语言·matlab
开发者小天4 小时前
python中For Loop的用法
java·服务器·python
flushmeteor4 小时前
JDK源码-基础类-String
java·开发语言
绾樘5 小时前
RHCE--基于Nginx的Web服务器配置
运维·服务器·nginx
生活很暖很治愈5 小时前
Linux基础开发工具
linux·服务器·git·vim
老百姓懂点AI5 小时前
[RAG实战] 向量数据库选型与优化:智能体来了(西南总部)AI agent指挥官的长短期记忆架构设计
python
u0109272715 小时前
C++中的策略模式变体
开发语言·c++·算法
雨季6666 小时前
构建 OpenHarmony 简易文字行数统计器:用字符串分割实现纯文本结构感知
开发语言·前端·javascript·flutter·ui·dart
雨季6666 小时前
Flutter 三端应用实战:OpenHarmony 简易倒序文本查看器开发指南
开发语言·javascript·flutter·ui