Search code examples
pythonhtmlflask

How to link external image in Flask


I am designing a website using Flask. Currently, I am trying to render a about us webpage using render_template. I have a manage.py in classified ads/back end/manage.py. The about.html sits in classified ads/front end/templates/about.html. The logo image is in classified ads/front end/assets/image/logo.png. The logo is supposed to be at the header and it links to homepage. However, when running flask --app manage run --debug, the browser does not show the logo. It gives a 404 for the logo image not found error in console.

//manage.py
from flask import Flask, render_template

app = Flask(__name__,template_folder="../front end/templates")

@app.route("/about")
def about():
    return render_template("about.html")

if __name__ == "__main__":    
    app.run(debug=True)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>MyListing16 - About us</title>
  </head>
  <body>
    <header class="header">
      <div class="header-top">
        <div class="logo">
          <a href="/front end"
            ><img src="/front end/assets/image/logo.png" alt=""
          /></a>
        </div>
      </div>
    </header>
    <h3>about</h3>
  </body>
</html>
![logo](https://i.sstatic.net/f5KkPzP6.png)

![about.html](https://i.sstatic.net/8aFNrJTK.png)

![file path](https://i.sstatic.net/26fKXRLM.png)

I changed the img src attributes from relative path ../assets/image/logo.png to full path /front end/assets/image/logo.png. Still, the logo does not show up.


Solution

  • Put image in front end/static folder, no assest ( front end/static/logo.png ) (If it doesn't works please share your python code)