news 2026/1/26 12:59:01

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/1/26 23:05:22

Portainer高效CI/CD流水线完整指南:从基础部署到进阶自动化

Portainer高效CI/CD流水线完整指南&#xff1a;从基础部署到进阶自动化 【免费下载链接】portainer Portainer: 是一个开源的轻量级容器管理 UI&#xff0c;用于管理 Docker 和 Kubernetes 集群。它可以帮助用户轻松地部署、管理和监控容器&#xff0c;适合用于运维和开发团队。…

作者头像 李华
网站建设 2026/1/23 21:33:12

3大架构级纹理优化策略:从内存瓶颈到性能突破的实战复盘

3大架构级纹理优化策略&#xff1a;从内存瓶颈到性能突破的实战复盘 【免费下载链接】raytracing.github.io Main Web Site (Online Books) 项目地址: https://gitcode.com/GitHub_Trending/ra/raytracing.github.io 在光线追踪项目的架构演进中&#xff0c;内存瓶颈往往…

作者头像 李华
网站建设 2026/1/22 15:55:17

Obsidian视觉定制完全指南:从功能增强到界面美化

Obsidian视觉定制完全指南&#xff1a;从功能增强到界面美化 【免费下载链接】awesome-obsidian &#x1f576;️ Awesome stuff for Obsidian 项目地址: https://gitcode.com/gh_mirrors/aw/awesome-obsidian 还在为Obsidian的默认界面感到单调乏味吗&#xff1f;想要打…

作者头像 李华
网站建设 2026/1/23 7:34:13

如何快速美化macOS光标:Mousecape新手完整教程

如何快速美化macOS光标&#xff1a;Mousecape新手完整教程 【免费下载链接】Mousecape Cursor Manager for OSX 项目地址: https://gitcode.com/gh_mirrors/mo/Mousecape Mousecape是一款专为macOS设计的鼠标光标主题管理器&#xff0c;让用户能够轻松自定义系统光标样式…

作者头像 李华
网站建设 2026/1/26 3:02:37

终极指南:5分钟掌握Codex多AI引擎灵活切换

终极指南&#xff1a;5分钟掌握Codex多AI引擎灵活切换 【免费下载链接】codex 为开发者打造的聊天驱动开发工具&#xff0c;能运行代码、操作文件并迭代。 项目地址: https://gitcode.com/GitHub_Trending/codex31/codex 还在为不同开发任务需要频繁切换AI模型而烦恼吗&…

作者头像 李华
网站建设 2026/1/25 17:46:10

学习Java26天

1. String 概述核心概念Java API&#xff1a;Java 提供的一套预定义类和接口&#xff0c;可以直接使用String 类&#xff1a;java.lang.String 代表字符串&#xff0c;程序中所有字符串字面值都是该类的对象重要特性&#xff1a;String 对象是不可变的&#xff08;immutable&am…

作者头像 李华