NobleScripts
NobleScripts

Reputation: 53

changing html image based on passed data by python with flask

return render_template('homepage.html',imgName=filenameD)

PYTHON

<img src= {{ name }} alt="something" style="width:500px;height:600px;">

HTML

im trying to change the image on my website based on the data I pass to it with python flask but the image does not show up, im using something called jinja?

Upvotes: 2

Views: 431

Answers (2)

IoaTzimas
IoaTzimas

Reputation: 10624

Try this:

Python

return render_template('homepage.html',name=filename)

HTML

<img src = "{{url_for('static', filename=name) }}" alt="something" style="width:500px;height:600px;">

Your image must be located inside 'static' folder and named as usually ('myimg.png', etc)

Upvotes: 1

Simeon Nedkov
Simeon Nedkov

Reputation: 1094

The image is not shown because you are referencing a non-existent variable in your template. Change your <img> tag to

<img src="{{ imgName }}" alt="something" style="width:500px;height:600px;">

and make sure that filenameD contains the path to your image and not only its name i.e. it should be something like /static/image.png.

Also, always surround your attribute values with "" to prevent XSS attacks, see Flask's security docs.

Upvotes: 1

Related Questions