Flask中URL构建
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于Flask中URL构建的主要内容,如果未能解决你的问题,请参考以下文章