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)
相关推荐
JELEE.2 小时前
Django登录注册完整代码(图片、邮箱验证、加密)
前端·javascript·后端·python·django·bootstrap·jquery
孫治AllenSun3 小时前
【算法】图相关算法和递归
windows·python·算法
读研的武6 小时前
DashGo零基础入门 纯Python的管理系统搭建
开发语言·python
Andy7 小时前
Python基础语法4
开发语言·python
mm-q29152227297 小时前
Python+Requests零基础系统掌握接口自动化测试
开发语言·python
电院工程师8 小时前
SIMON64/128算法Verilog流水线实现(附Python实现)
python·嵌入式硬件·算法·密码学
Python图像识别10 小时前
75_基于深度学习的咖啡叶片病害检测系统(yolo11、yolov8、yolov5+UI界面+Python项目源码+模型+标注好的数据集)
python·深度学习·yolo
闲人编程10 小时前
Python游戏开发入门:Pygame实战
开发语言·python·游戏·pygame·毕设·codecapsule
雍凉明月夜11 小时前
人工智能学习中深度学习之python基础之 类
python·学习