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)
相关推荐
Yan-英杰15 分钟前
百度搜索和文心智能体接入DeepSeek满血版——AI搜索的新纪元
图像处理·人工智能·python·深度学习·deepseek
weixin_307779131 小时前
Azure上基于OpenAI GPT-4模型验证行政区域数据的设计方案
数据仓库·python·云计算·aws
玩电脑的辣条哥2 小时前
Python如何播放本地音乐并在web页面播放
开发语言·前端·python
多想和从前一样5 小时前
Django 创建表时 “__str__ ”方法的使用
后端·python·django
小喵要摸鱼6 小时前
【Pytorch 库】自定义数据集相关的类
pytorch·python
bdawn6 小时前
深度集成DeepSeek大模型:WebSocket流式聊天实现
python·websocket·openai·api·实时聊天·deepseek大模型·流式输出
Jackson@ML6 小时前
Python数据可视化简介
开发语言·python·数据可视化
mosquito_lover16 小时前
怎么把pyqt界面做的像web一样漂亮
前端·python·pyqt
mengyoufengyu7 小时前
算法12-贪心算法
python·算法·贪心算法
T_Y99437 小时前
pythonrsa加密与sha256加密
python