Thiago
Thiago

Reputation: 672

Send GET parameter on url_for inside HTML

I have the following code:

<ul>
    {% for name in items %}
        <li style="list-style-type: none;">
            <a href="{{ url_for('content', values='{{name}}') }}">{{ items[name].title }}</a>
        </li>
    {% endfor %}
</ul>

I want to insert into the url_for a GET parameter that will be the name of the item. When I get the value sent to my views.py, I get the whole string '{{name}}'.

@app.route('/content', methods=['GET', 'POST'])
def content():
    name = request.args.get('values')
    print name

>> {{name}}

I've tried concatenating with + like {{ url_for('content', values='"+{{name}}+"') }}">{{ items[name].title }}, but it didn't work too.

Any clues on how to do that? Is it possible to send a POST param that way?

Upvotes: 5

Views: 7085

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124238

Just use the variable name directly in the expression:

{{ url_for('content', values=name) }}

'{{name}}' is a literal string with the characters {, {, n, etc, not an interpolation of the variable.

Note that this is not a POST parameter; it is a URL query parameter (part of the URL after the ?).

Upvotes: 13

Related Questions