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)
相关推荐
u***32434 小时前
使用python进行PostgreSQL 数据库连接
数据库·python·postgresql
青瓷程序设计6 小时前
动物识别系统【最新版】Python+TensorFlow+Vue3+Django+人工智能+深度学习+卷积神经网络算法
人工智能·python·深度学习
tobebetter95276 小时前
How to manage python versions on windows
开发语言·windows·python
F_D_Z6 小时前
数据集相关类代码回顾理解 | sns.distplot\%matplotlib inline\sns.scatterplot
python·深度学习·matplotlib
daidaidaiyu7 小时前
一文入门 LangGraph 开发
python·ai
不知更鸟8 小时前
前端报错:快速解决Django接口404问题
前端·python·django
4***72138 小时前
【玩转全栈】----Django模板语法、请求与响应
数据库·python·django
梁正雄8 小时前
1、python基础语法
开发语言·python
ituff9 小时前
微软认证考试又免费了
后端·python·flask
梁正雄10 小时前
2、Python流程控制
开发语言·python