Process ampersand in url in bulb

I am new to web development using the Flask and Jinja2 templates. One column in a row of my HTML table in a template:

<td><a> href="/plot?label={{stats[0]}}">{{stats[0]}} </a></td>

stats[0]is a variable that is a string and can contain '&' for example, Eggs and Spam. Now in the view (views.py):

@app.route("/plot", methods = ["GET", "POST"])
@login_required
def on_plot():
    data = request.args
    label_name = data['label']

In this case, I get datahow ImmutableMultiDict([(' Spam', u''), ('label', u'Eggs ')]) that is not true. Instead i wantImmutableMultiDict([('label', u'Eggs & Spam ')])

So how do I handle this case? I tried to do {{stats[0]|escape}}, but it does not work.

+4
source share
1 answer

Instead, you want to create a URL from the view using url_for:

<td><a> href="{{ url_for('on_plot', label=stats[0]) }}">{{stats[0]}} </a></td>

url_for() .

+4

All Articles