favicon.rst
This file
Path
docs/patterns/favicon.rst
56 lines · 2.1 KiB
Last changed by
Merge branch 'stable'David Lordbefore adoptionb78b5a2
History of this fileTake it
The walk stopped after 200 commits: lines older than 73739a2 point at it.
Merge branch '2.2.x'David Lordbefore adoption73739a2
lines 1–26why, one step back1Adding a favicon2================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::26Merge branch 'stable'David Lordbefore adoptionb78b5a2
lines 27–31why, one step back27 app.add_url_rule(28 "/favicon.ico",29 endpoint="favicon",30 redirect_to=url_for("static", filename="favicon.ico"),31 )Merge branch '2.2.x'David Lordbefore adoption73739a2
lines 32–56why, one step back3233If you want to save the extra redirect request you can also write a view34using :func:`~flask.send_from_directory`::3536 import os37 from flask import send_from_directory3839 @app.route('/favicon.ico')40 def favicon():41 return send_from_directory(os.path.join(app.root_path, 'static'),42 'favicon.ico', mimetype='image/vnd.microsoft.icon')4344We can leave out the explicit mimetype and it will be guessed, but we may45as well specify it to avoid the extra guessing, as it will always be the46same.4748The above will serve the icon via your application and if possible it's49better to configure your dedicated web server to serve it; refer to the50web server's documentation.5152See also53--------5455* The `Favicon <https://en.wikipedia.org/wiki/Favicon>`_ article on56 Wikipedia