Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use Flask Jinja2 url_for with multiple parameters

I have a problem while using jinja2 url_for() function.

I have a route like this:

@app.route('/article/<int:article_id>/<url_title>/', methods=['GET'])
def article_page(article_id, url_title):
    article = Article.query.get(article_id)
    if article == None:
        abort(404)
    return render_template('article.html', article=article)

in jinja template file,i want to create a url which links to article_page,so i write like this:

<h5>
  <a href="{{ url_for('article_page',article_id=article.id,url_title=article.url_title) }}">{{ article.title }}</a>
</h5>

but when I run this page, I get a error:

raise BuildError(endpoint, values, method)
BuildError: ('article_page', {'article_id': 1}, None)

It seems like that the second parameter url_title missing. How can I use url_for() with multiple parameters correctly?

like image 600
Tiny Lin Avatar asked Dec 28 '13 03:12

Tiny Lin


1 Answers

According to url_for documentation:

If the value of a query argument is None, the whole pair is skipped.

Make sure that url_title is not None.

Or specify default value for url_title in the article_page function.

like image 161
falsetru Avatar answered Sep 20 '22 09:09

falsetru