news 2026/8/15 4:02:03

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Python Flask 开发 - Flask 路径参数类型(string、int、float、path、uuid)

查看路径参数类型

forconverter_name,converter_classinapp.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

@app.route('/user/<string:username>')defshow_string(username):returnf'type: string, value:{username}, python_type:{type(username).__name__}'
  1. 测试/user/john_doe
# 输出结果 type: string, value: john_doe, python_type: str
  1. 测试/user/123
# 输出结果 type: string, value: 123, python_type: str
  1. 测试/user/john%20doe
# 输出结果 type: string, value: john doe, python_type: str

二、int

@app.route('/article/<int:article_id>')defshow_int(article_id):returnf'type: int, value:{article_id}, python_type:{type(article_id).__name__}'
  1. 测试/article/42
# 输出结果 type: int, value: 42, python_type: int
  1. 测试/article/0
# 输出结果 type: int, value: 0, python_type: int
  1. 测试/article/-100
# 输出结果 Not Found
  1. 测试/article/3.14
# 输出结果 Not Found
  1. 测试/article/abc
# 输出结果 Not Found
  1. 测试/article/10abc
# 输出结果 Not Found

三、float

@app.route('/price/<float:price>')defshow_float(price):returnf'type: float, value:{price}, python_type:{type(price).__name__}'
  1. 测试/price/19.99
# 输出结果 type: float, value: 19.99, python_type: float
  1. 测试/price/-5.5
# 输出结果 Not Found
  1. 测试/price/100
# 输出结果 Not Found
  1. 测试/price/100.0
# 输出结果 type: float, value: 100.0, python_type: float
  1. 测试/price/.5
# 输出结果 Not Found
  1. 测试/price/3.1415926
# 输出结果 type: float, value: 3.1415926, python_type: float
  1. 测试/price/10,5
# 输出结果 Not Found
  1. 测试/price/10k
# 输出结果 Not Found
  1. 测试/price/abc
# 输出结果 Not Found

四、path

1、演示
@app.route('/path/<path:filepath>')defshow_path(filepath):returnf'type: path, value:{filepath}, python_type:{type(filepath).__name__}'
  1. 测试/path/docs
# 输出结果 type: path, value: docs, python_type: str
  1. 测试/path/docs/api
# 输出结果 type: path, value: docs/api, python_type: str
  1. 测试/path/docs/api/v1
# 输出结果 type: path, value: docs/api/v1, python_type: str
  1. 测试/path/static/css/style.css
# 输出结果 type: path, value: static/css/style.css, python_type: str
  1. 测试/path/a/b/../d
# 输出结果 type: path, value: a/b/../d, python_type: str
2、string 处理路径类型的问题
@app.route('/path/<string:filepath>')defshow_path(filepath):returnf'type: string, value:{filepath}, python_type:{type(filepath).__name__}'
  1. 测试/path/docs
# 输出结果 type: string, value: docs, python_type: str
  1. 测试/path/docs/api
# 输出结果 Not Found
  1. 测试/path/docs/api/v1
# 输出结果 Not Found
  1. 测试/path/static/css/style.css
# 输出结果 Not Found
  1. 测试/path/a/b/../d
# 输出结果 Not Found

五、uuid

@app.route('/resource/<uuid:resource_id>')defshow_uuid(resource_id):returnf'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
  1. 测试/resource/550e8400-e29b-41d4-a716-446655440000
# 输出结果 type: uuid, value: 550e8400-e29b-41d4-a716-446655440000, python_type: UUID
  1. 测试/resource/not-a-uuid
# 输出结果 Not Found
  1. 测试/resource/123
# 输出结果 Not Found
  1. 测试/resource/123e4567-e89b-12d3-a456-42661417400(短了)
# 输出结果 Not Found
  1. 测试/resource/123e4567-e89b-12d3-a456-4266141740000(长了)
# 输出结果 Not Found
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/8/14 20:21:21

10款免费QSS模板快速美化你的Qt应用界面

10款免费QSS模板快速美化你的Qt应用界面 【免费下载链接】QSS QT Style Sheets templates 项目地址: https://gitcode.com/gh_mirrors/qs/QSS 想让你的Qt应用界面告别单调乏味&#xff0c;瞬间变得专业又美观吗&#xff1f;QSS模板库为你提供了简单高效的解决方案。即使…

作者头像 李华
网站建设 2026/8/13 19:19:57

重塑移动AI未来:ANDROIDWORLD如何彻底改变Android自主代理评估基准

重塑移动AI未来&#xff1a;ANDROIDWORLD如何彻底改变Android自主代理评估基准 【免费下载链接】androidgen-glm-4-9b 项目地址: https://ai.gitcode.com/zai-org/androidgen-glm-4-9b 您是否曾经遇到过这样的情况&#xff1a;精心研发的移动AI代理在实验室表现优异&am…

作者头像 李华
网站建设 2026/8/14 9:58:24

如何淡化功利心守护初心呢

朋友的分享如下这段是一个朋友分享给我的。这段回复其实就说明的教育的无效性&#xff0c;或者委婉一些&#xff0c;教育是非常低效率的。改变自己都难&#xff0c;改变自我那是神……改变别人更难&#xff0c;改变他人是“深井冰”……改变环境……天哪&#xff0c;这人在想啥…

作者头像 李华
网站建设 2026/8/14 20:29:46

畅享高效体验:手把手教你玩转Gemini3与Nano Banana Pro

在现代数字世界中&#xff0c;效率就是生产力。当你拥有Gemini3和Nano Banana Pro这样的高性能设备时&#xff0c;如果掌握得当&#xff0c;它们可以成为你工作和学习中不可或缺的高效助手。无论是开发者、创客&#xff0c;还是对技术感兴趣的爱好者&#xff0c;通过合理配置和…

作者头像 李华
网站建设 2026/8/14 23:29:36

Mooncake终极指南:开启语言模型服务新纪元

Mooncake终极指南&#xff1a;开启语言模型服务新纪元 【免费下载链接】Mooncake 项目地址: https://gitcode.com/gh_mirrors/mo/Mooncake 你是否曾为语言模型服务的性能瓶颈而烦恼&#xff1f;Mooncake正是为解决这一痛点而生的创新解决方案。作为Moonshot AI推出的开…

作者头像 李华
网站建设 2026/8/15 3:47:39

吃透 JS 深浅拷贝:从原理到实战,避坑指南全解析

在前端开发中&#xff0c;“拷贝对象 / 数组” 是高频操作 —— 比如修改表单数据时保留原始值、状态管理中避免副作用、处理接口返回数据时隔离修改。但很多开发者只知 “浅拷贝” 和 “深拷贝” 之名&#xff0c;却踩遍引用传递的坑&#xff1a;修改新对象&#xff0c;原对象…

作者头像 李华