skip to Main Content

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

2

Answers


  1. 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">
    
    Login or Signup to reply.
  2. 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">
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search