hzxscyq
hzxscyq

Reputation: 13

I want to use Jinja2 url_for function with two parameters and got some problems

this is my code:

@app.route('/user_article/<username>/<title>')
    def user_article(username,title):
        db = get_db()
        cu = db.cursor()
        cu.execute("select *from article where username='%s',title='%s'" %(username,title))
        data = cu.fetchone()
        article = data[2]
        return render_template('user_article.html',article=article)

and my html code:

{% for data in user_article %}
<p>
    <a href='{{ url_for("user_article",username={{data[0]}},title={{data[1]}}) }}'>{{data[1]}}</a>
</p>
{% endfor %}

when i run the code i got an error: jinja2.exceptions.TemplateSyntaxError: expected token ':', got '}'

what should i do to get the parameters?thanks

Upvotes: 0

Views: 404

Answers (1)

user5437241
user5437241

Reputation:

This line should be without double brackets

url_for("user_article",username=data[0],title=data[1])

html code

{% for data in user_article %}
<p>
    <a href='{{ url_for("user_article",username=data[0],title=data[1]) }}'>{{data[1]}}</a>
</p>
{% endfor %}

Upvotes: 1

Related Questions