URL中两种方式传参

@app.route('/')
def hello_world():
    return 'Hello World!'

@app.route('/list/')
def article_list():
    return 'article list'


传递参数需要使用的是'/<参数名>/'。
然后在试图函数中,也要定义同名的参数

@app.route('/p/<article_id>/')
def article_detaile(article_id):
    return '您请求的文章是:%s'%article_id

如果没有指定具体的数据类型,那么默认就是使用'string'数据类型
指定参数类型:string int float path uuid any
如:

@app.route('/p/int:<article_id>/')
def article_detaile(article_id):
    return '您请求的文章是:%s'%article_id

uuid的使用

import uuid
@app.route('/u/<uuid:user_id>/')
def user_detail(user_id):
    return '用户个人中心页面: %s' %user_id
print(uuid.uuid4())

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

补充:像百度搜索引擎一样的方式

@app.route('/d/')
def d():
    wd = request.args.get('wd')
    return '您通过查询字符串传递的参数是: %s' % wd
if __name__ == '__main__':
    app.run(debug=True)
接受用户传递的参数:
1.使用path形式(将参数嵌入到路径中)
2.使用查询字符串的方式,就是通过'?key=value'的心事传递
如果页面想要做seo优化,那么用第一种,如果不想被搜索引擎发现,那么用另一种



猜你喜欢

转载自www.cnblogs.com/rcat/p/9638334.html
今日推荐