Flask的URL传参方式有哪些

url传参方式

普通传参方式

@app.route('/p/<id>/')
def article_detail(id):
    return '你访问的文章第%s篇'%id

z.png

指定参数类型

有以下几种类型:

string:默认的数据类型

int:接受整形

float:浮点型

path:和string的类似,但是接受斜杠

any:可以指定多个路径

uuid:只接受uuid字符串

相关推荐:《Python相关教程》

 (1)any

@app.route('/<any(blog,user):url_path>/<id>')
def detail(url_path,id):
    if url_path == 'blog':
        return '博客详情%s'%id
    else:
        return '用户详情%s'%id

x.png

cc.png

 (2)path

@app.route('/article/<path:test>/')
def test_article(test):
    return 'test_article:{}'.format(test)

vv.png

获取参数

from flask import Flask,request
@app.route('/tieba/')
def tieba():
    wd = request.args.get('wd')
    return '获取的参数的是%s'%wd

bb.png

来源:PY学习网:原文地址:https://www.py.cn/article.html

hmoban主题是根据ripro二开的主题,极致后台体验,无插件,集成会员系统
自学咖网 » Flask的URL传参方式有哪些