2
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?

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.