问题:使用url_for()在Flask中创建动态URL

我的Flask路线中有一半需要使用变量say /<variable>/add/<variable>/remove。如何创建到这些位置的链接?

url_for() 需要一个参数传递给函数,但是我不能添加参数?

Half of my Flask routes requires a variable say, /<variable>/add or /<variable>/remove. How do I create links to those locations?

url_for() takes one argument for the function to route to but I can’t add arguments?


回答 0

它使用关键字参数作为变量:

url_for('add', variable=foo)

It takes keyword arguments for the variables:

url_for('add', variable=foo)

回答 1

url_forFlask中的ins用于创建URL,以防止必须在整个应用程序(包括模板)中更改URL的开销。如果不使用url_for,则如果您的应用程序的根URL发生更改,则必须在存在该链接的每个页面中进行更改。

句法: url_for('name of the function of the route','parameters (if required)')

它可以用作:

@app.route('/index')
@app.route('/')
def index():
    return 'you are in the index page'

现在,如果您有索引页的链接,则可以使用此页面:

<a href={{ url_for('index') }}>Index</a>

您可以用它做很多事情,例如:

@app.route('/questions/<int:question_id>'):    #int has been used as a filter that only integer will be passed in the url otherwise it will give a 404 error
def find_question(question_id):  
    return ('you asked for question{0}'.format(question_id))

对于以上内容,我们可以使用:

<a href = {{ url_for('find_question' ,question_id=1) }}>Question 1</a>

这样,您可以简单地传递参数!

url_for in Flask is used for creating a URL to prevent the overhead of having to change URLs throughout an application (including in templates). Without url_for, if there is a change in the root URL of your app then you have to change it in every page where the link is present.

Syntax: url_for('name of the function of the route','parameters (if required)')

It can be used as:

@app.route('/index')
@app.route('/')
def index():
    return 'you are in the index page'

Now if you have a link the index page:you can use this:

<a href={{ url_for('index') }}>Index</a>

You can do a lot o stuff with it, for example:

@app.route('/questions/<int:question_id>'):    #int has been used as a filter that only integer will be passed in the url otherwise it will give a 404 error
def find_question(question_id):  
    return ('you asked for question{0}'.format(question_id))

For the above we can use:

<a href = {{ url_for('find_question' ,question_id=1) }}>Question 1</a>

Like this you can simply pass the parameters!


回答 2

请参阅Flask API文档以获取flask.url_for()

下面是用于将js或css链接到模板的其他用法示例片段。

<script src="{{ url_for('static', filename='jquery.min.js') }}"></script>

<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">

Refer to the Flask API document for flask.url_for()

Other sample snippets of usage for linking js or css to your template are below.

<script src="{{ url_for('static', filename='jquery.min.js') }}"></script>

<link rel=stylesheet type=text/css href="{{ url_for('static', filename='style.css') }}">

回答 3

范本:

传递函数名称和参数。

<a href="{{ url_for('get_blog_post',id = blog.id)}}">{{blog.title}}</a>

查看功能

@app.route('/blog/post/<string:id>',methods=['GET'])
def get_blog_post(id):
    return id

Templates:

Pass function name and argument.

<a href="{{ url_for('get_blog_post',id = blog.id)}}">{{blog.title}}</a>

View,function

@app.route('/blog/post/<string:id>',methods=['GET'])
def get_blog_post(id):
    return id

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。