Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
506 views
in Technique[技术] by (71.8m points)

python 3.x - How do I pass a url from a Jinja2 template to a Flask app route / function whea a parameter contains whitespace

I would like to pass a parameter to a Flask app from a Jinja2 template that contains white space. In the example below I am trying to pass Dallas Cowboys.

template

{% set homeTeam = 'Dallas Cowboys'%}
{% set league = 'nfl' %}
{% set sport = 'football' %}

{% set url = '/details/' + sport + '/' + league + '/' + homeTeam %}
<a href={{url}}>{{time_formatted}} - {{homeTeam}} vs. {{awayTeam}}</a>
             
<a href={{url}}>Click Me</a> 

What I want here is:

<a href="/details/football/nfl/dallas%20cowboys>Click Me</a>

What I get is:

<a href="/details/football/nfl/dallas" cowboys?="">Click Me</a>

I want to be able to print "Dallas Cowboys" in the Flask function below:

app

@app.route('/details/<sport>/<league>/<home_team>')
    def details(sport, league, home_team):
        print(sport, league, home_team) #I need "Dallas Cowbows" for home_team
        return render_template('details.html', sport=sport, league=league, home_team=home_team)
question from:https://stackoverflow.com/questions/65920086/how-do-i-pass-a-url-from-a-jinja2-template-to-a-flask-app-route-function-whea

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Template

<a href='{{url_for('details', sport='football', league='nfl, home_team='Cowboys' > Click </a>

This should work , note use url_for , pass all the parameters to backend and thats it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...