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 小时前
五、《从零手撸 Agent》 — 什么是 Agent:从“会聊天”到“会做事”,差的就是一个循环
python·agent
梯度下降者4 小时前
Python - 函数
python·高阶函数·可调用对象·functools·嵌套作用域
starzy19906 小时前
深入理解Scala隐式参数:上下文流转
开发语言·python·scala
wuyk5557 小时前
Spider-第16讲-线程池与竞态问题
大数据·爬虫·python·elasticsearch·搜索引擎
kyle~13 小时前
FastAPI --- 将业务能力以 API 的形式提供给外部系统
python·fastapi·前后端
aiotto资源分享13 小时前
Python 写 JSON 文件,程序中途退出会留下什么?
java·python·json·jsondecodeerror
梯度下降者14 小时前
Python信号量,条件变量,事件学习
python·条件变量·事件·信号量·线程通信
MSTcheng.14 小时前
【AI应用实战】用 FastAPI + SSE 做一个自己的 AI 聊天网页:流式输出、多轮对话与完整源码
人工智能·python·大模型·fastapi·sse·ai聊天助手
the局外人15 小时前
Python 实战检索优化:BM25、向量与重排
数据库·后端·python
__underline__15 小时前
冷门Python知识点006篇:Python 类体里的名字,能被"凭空造出来"
后端·python