6

i am building a webpage application using Flask, Jinja2 and Bootstrap. When running the app i get this error:

* Running on http://localhost:53293/ (Press CTRL+C to quit)
127.0.0.1 - - [20/Jun/2015 19:33:56] "GET / HTTP/1.1" 500 -
127.0.0.1 - - [20/Jun/2015 19:33:56] "GET /favicon.ico HTTP/1.1" 404 -

The app is based on the Visual Studio 2015rc python template Flask Web Project which worked fine.

None of my changes included a favicon, i searched the complete project including external files for favicon, icon, .ico but found nothing.

Does someone have an idea how this error could be caused or how to locate the code requesting the favicon?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Niels
  • 469
  • 2
  • 6
  • 18

2 Answers2

7

Browsers always try to find a favicon for a site by looking in a standard location, /favicon.ico. You did nothing wrong here.

You could of course include serve a file at that location, if you so wish.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • 2
    Worked using this Code: `import os from flask import send_from_directory @app.route('/favicon.ico') def favicon(): return send_from_directory(os.path.join(app.root_path, 'static'), 'favicon.ico', mimetype='image/vnd.microsoft.icon')` from [Flask](http://flask.pocoo.org/docs/0.10/patterns/favicon/) – Niels Jun 20 '15 at 19:01
3

Add an icon to use as your favorite icon (my_favicon.png in this example) in the static/images folder.

Option 1 (Simple and solves the problem). Add to your routes:

from flask import (url_for, current_app as app)

# favicon
@app.route('/favicon.ico')
def favicon():
    return url_for('static', filename='/images/my_favicon.png')

Option 2: (more general, use solution suggested in the flask docs using send_from_directory as Niels also suggests.

Note: If you want to actually serve the icon and have it show up in the browser tab, add the following to your base template:

<link rel="shortcut icon" href="{{ url_for('static', filename='images/my_favicon.png') }}">
Chuck Tucker
  • 259
  • 1
  • 8
  • The snippet given above by Niels is more consistent (if the file is a png the path should serve a png too). Overall, url routing should be generalized enough to serve static content. Avoid dedicated one only for a single file. – Flint May 21 '21 at 15:18