Flask中URL构建

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

猜你喜欢

转载自blog.51cto.com/huanghai/2149220