Skip to content
Myr

favicon.rst

This file

Path

docs/patterns/favicon.rst

53 lines · 2.0 KiB

Last changed by

Not known.

History of this file

Take it

The walk stopped after 200 commits: lines older than 41aaaf7 point at it.

clean up unused function from prefixed_env prDavid Lordbefore adoption41aaaf7

lines 1–53why, one step back
1Adding a favicon
2================34A "favicon" is an icon used by browsers for tabs and bookmarks. This helps5to distinguish your website and to give it a unique brand.67A common question is how to add a favicon to a Flask application. First, of8course, you need an icon. It should be 16 × 16 pixels and in the ICO file9format. This is not a requirement but a de-facto standard supported by all10relevant browsers. Put the icon in your static directory as11:file:`favicon.ico`.1213Now, to get browsers to find your icon, the correct way is to add a link14tag in your HTML. So, for example:1516.. sourcecode:: html+jinja1718 <link rel="shortcut icon" href="{{ url_for('static', filename='favicon.ico') }}">1920That's all you need for most browsers, however some really old ones do not21support this standard. The old de-facto standard is to serve this file,22with this name, at the website root. If your application is not mounted at23the root path of the domain you either need to configure the web server to24serve the icon at the root or if you can't do that you're out of luck. If25however your application is the root you can simply route a redirect::2627 app.add_url_rule('/favicon.ico',28 redirect_to=url_for('static', filename='favicon.ico'))2930If you want to save the extra redirect request you can also write a view31using :func:`~flask.send_from_directory`::3233 import os34 from flask import send_from_directory3536 @app.route('/favicon.ico')37 def favicon():38 return send_from_directory(os.path.join(app.root_path, 'static'),39 'favicon.ico', mimetype='image/vnd.microsoft.icon')4041We can leave out the explicit mimetype and it will be guessed, but we may42as well specify it to avoid the extra guessing, as it will always be the43same.4445The above will serve the icon via your application and if possible it's46better to configure your dedicated web server to serve it; refer to the47web server's documentation.4849See also50--------5152* The `Favicon <https://en.wikipedia.org/wiki/Favicon>`_ article on53 Wikipedia