使用 url_for() 在 Flask 中创建动态 URL [英] Create dynamic URLs in Flask with url_for()
本文介绍了使用 url_for() 在 Flask 中创建动态 URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 Flask 路由中有一半需要一个变量,比如 /
或 /
.如何创建指向这些位置的链接?
Half of my Flask routes requires a variable say, /<variable>/add
or /<variable>/remove
. How do I create links to those locations?
url_for()
为要路由到的函数采用一个参数,但我无法添加参数?
url_for()
takes one argument for the function to route to but I can't add arguments?
推荐答案
它接受变量的关键字参数:
It takes keyword arguments for the variables:
url_for('add', variable=foo)
url_for('remove', variable=foo)
flask-server 将具有以下功能:
The flask-server would have functions:
@app.route('/<variable>/add', methods=['GET', 'POST'])
def add(variable):
@app.route('/<variable>/remove', methods=['GET', 'POST'])
def remove(variable):
这篇关于使用 url_for() 在 Flask 中创建动态 URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文