I am trying to load an image through static but getting 404 error.
Here’s my code
<img src="{{url_for('static', filename=images/cat.png)}}" alt="new image">
<img src="{{url_for('static' filename=photo.png)}}" alt="new photo">
all my filepaths seem to be correct but the image is still not loading. When i just use the normal way of putting an image in HTML, the image shows up. for ex –
<img src="/static/images/cat.png" alt="photo Image">
here’s the filepaths of the project in pycharm
Aman is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I recommend to use Direct Path
rather than url_for
in this case, and it would look something like this:
<!-- Using direct path -->
<img src="/static/images/cat.png" alt="photo Image">
<img src="/static/photo.png" alt="new photo">
However if you insist on using url_for
, You need to use the correct syntax:
<img src="{{ url_for('static', filename='images/cat.png') }}" alt="new image">
<img src="{{ url_for('static', filename='photo.png') }}" alt="new photo">
You have an issue with your syntax, specifically with the quotes. Try this code instead:
<img src="{{ url_for('static', filename='images/casual.JPG') }}" alt="new image">
<img src="{{ url_for('static', filename='photo.png') }}" alt="new photo">