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)
相关推荐
小白学大数据3 分钟前
Python 进阶爬虫:解析知识星球 API
开发语言·爬虫·python
编码者卢布4 分钟前
【Azure Developer】azd 安装最新版无法登录中国区问题二:本地Windows环境遇问题
microsoft·flask·azure
whale fall6 分钟前
如何在同一台电脑里安装32 位 Python 和 64 位 Python
开发语言·笔记·python·学习
SNAKEpc121388 分钟前
PyQtGraph应用(五):k线回放复盘功能实现
python·qt·pyqt
2401_841495649 分钟前
【Python高级编程】近似串匹配
python·算法·动态规划·字符串·数组·时间复杂度·空间复杂度
历程里程碑20 分钟前
滑动窗口------滑动窗口最大值
大数据·python·算法·elasticsearch·搜索引擎·flask·tornado
AI_567823 分钟前
Python正则表达式终极指南:从模式匹配到文本工程的智能跃迁
人工智能·python·正则表达式
B站_计算机毕业设计之家23 分钟前
AI大模型:Deepseek美食推荐系统 机器学习 协同过滤推荐算法+可视化 Django框架 大数据毕业设计(源码)✅
python·算法·机器学习·数据分析·django·推荐算法·美食
黄筱筱筱筱筱筱筱23 分钟前
第三次Python练习题
开发语言·python
2501_9413220328 分钟前
叉车人员检测识别实战:基于YOLOv8-BiFPN的高精度目标检测方案
python·yolo·目标检测