Reputation: 33
So i am trying to nest 2 variables in a url_for
to pull a specific persons photo.
This is my code
<img
class="imgContainer"
src="{{ url_for('static', filename='players/_{{player.username}}_.png') }}"
/>
My images are of the form 'username.png' so thats why I put the '_' at the beginning and end.
This is the Python part of it:
@app.route("/<username>")
def player(username):
player = Player.query.filter_by(username=username).first()
return render_template("player.html",player=player)
What I tried:
{{ url_for('static', filename='players/_[player][username]_.png') }}
{{ url_for('static', filename='players/_player[username]_.png') }}
Upvotes: 2
Views: 1899
Reputation: 21312
When setting up your url_for
, it's already contained in the {{ }}
. So, you can break up your string + variables like this:
<img class="imgContainer"
src="{{ url_for('static', filename='players/_' ~ player.username ~ '_.png') }}" />
Note the use of ~
to concatenate your variable and string within the {{ }}
syntax. Using the ~
will convert your values to string first. If you know your variable types you can use +
.
Upvotes: 5
Reputation: 26157
You can just concatenate the string using +
as you would in Python.
Before:
src="{{ url_for('static', filename='players/_{{player.username}}_.png') }}"
After:
src="{{ url_for('static', filename='players/' + player.username + '.png') }}"
Upvotes: 2