首页 > Web开发 > 详细

Flask中URL构建

时间:2018-07-23 23:30:16      阅读:322      评论:0      收藏:0      [点我收藏+]
Flask QuickStart Refence

URL Building

To build a URL to a specific function, use the url_for() function. It accepts the name of the function as its first argument and any number of keyword arguments, each corresponding to a variable part of the URL rule. Unknown variable parts are appended to the URL as query parameters.

Flask 快速参考相关(译文)

URL 构建

url_for() 函数用于构建指定函数的 URL。第一个参数为函数名,同时可以有多个对应于URL中变量的关键字参数,而未知变量将添加到 URL 中作为查询参数。

举例说明

from flask import Flask, url_for

app = Flask(__name__)

@app.route(‘/‘)
def index():
    return ‘index‘
@app.route(‘/login‘)
def login():
    return ‘login‘
@app.route(‘/user/<username>‘)
def profile(username):
    return ‘{}\‘s profile‘.format(username)

with app.test_request_context():
    print(url_for(‘index‘))
    print(url_for(‘login‘))
    print(url_for(‘login‘, next=‘/‘))
    print(url_for(‘profile‘, username=‘John Doe‘))

/
/login
/login?next=/
/user/John%20Doe

Flask中URL构建

原文:http://blog.51cto.com/huanghai/2149220

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!