Flask中URL构建
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_forapp = 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
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。