python之Flask入门—路由参数

语法:

'/routerName/<string:parameter_name>'

其中:routerName代表路由名称<>中的string是参数类型,parameter_name为参数名称

参数类型:

(1) string 接收任何没有斜杠('/')的字符串(默认)

python 复制代码
#下面以登录借口吧为列,login是路由名称,传递参数username,类型为String,省略类型默认就是字符串
#@blue.route('/login/<string:username>/')
@blue.route('/login/<username>/')
def get_string(username):
    print(type(username))  # <class 'str'>
    return username

(2) int 接收整型

python 复制代码
# int
@blue.route('/login/<int:id>/')
def get_int(id):
    print(type(id))  # <class 'int'>
    return str(id)

(3)float 接收浮点型

python 复制代码
#
@blue.route('/floatRouterName/<float:money>/')
def get_float(money):
    print(type(money))  # <class 'float'>
    return str(money)

(4)path 接收路径,可接收斜线('/')

(5) uuid 只接受uuid字符串,唯一码,一种生成规则

(6)any 可以同时指定多种路径,进行限定

python 复制代码
# any: 从列出的项目中选择一个
#下面routerName为路由名称(接口名称),接受一个any类型的参数fruit
#注意,这里表示从列出的项目中选择一个,也就是传递的参数只能是apple, orange, banana三选一
@blue.route('/routerName/<any(apple, orange, banana):fruit>/')
def get_any(fruit):
    print(type(fruit))  # <class 'str'>
    return str(fruit)
相关推荐
光景aigeo优化5 分钟前
geo贴牌厂家,是光景极欧科技
python·科技
rgeshfgreh15 分钟前
Python连接KingbaseES数据库全指南
开发语言·数据库·python
万行16 分钟前
机器人系统ros2&期末速通2
前端·人工智能·python·算法·机器学习
weixin_4624462316 分钟前
Python + FFmpeg 批量提取视频音频(支持 Windows / macOS / Linux)
python·ffmpeg·音视频
海棠AI实验室18 分钟前
第九章:函数接口设计:输入 / 输出 / 边界条件
python·接口设计
万行18 分钟前
机器人系统ros2&期末速通&1
人工智能·python·机器学习·机器人
轻竹办公PPT19 分钟前
AI 生成 2026 年工作计划 PPT,逻辑清晰度对比测试
人工智能·python·powerpoint
Direction_Wind24 分钟前
抖音视频下载,直播间监控,直播间发言采集,最新加密算法
python·node.js
旦莫31 分钟前
使用OCR加持的APP自动化测试
python·测试开发·自动化·ocr·pytest·ai测试
天若有情67333 分钟前
用 Python 爬取电商商品数据:从入门到反爬破解
开发语言·python