I need to set up a favicon for django admin interface.
It would be best to do it globally, without overriding templates for all apps.
What is the cleanest way to do it? I tried searching Django documentation for this, but found nothing.
Before embedding the favicon in web pages, it must be added to the Django project as a static file. Make sure the favicon is accessible however you choose to set up static files. The simplest approach would be to put the image file under a directory named static/images and use the standard static file settings.
Django favicon A favicon or favorite icon is the icon located on the left side of a browser tab. The generic favicon for Google and Firefox browser tabs is a globe icon. Websites often override this generic favicon and use their logo or an image associated with their site.
A favicon is a graphic image (icon) associated with a particular Web page and/or Web site. Many recent user agents (such as graphical browsers and newsreaders) display them as a visual reminder of the Web site identity in the address bar or in tabs.
If favicon is in /app/static/img/favicon.ico
, link it into the {% block extrahead %}
of this file: /app/templates/admin/base_site.html
{% extends "admin/base.html" %} {% block title %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %} {% block extrahead %} <link rel="icon" href="{{STATIC_URL}}img/favicon.ico" sizes="48x48" /> {% endblock %} {% block branding %} <h1 id="site-name"><a href="{% url 'admin:index' %}">{{ site_header|default:_('Django administration') }}</a></h1> {% endblock %}
In settings.py
, INSTALLED_APPS
be sure your app is listed before django.contrib.admin
.
To test get rid of template cache by deleting .pyc
files:
$ find . -name \"*.pyc\" -delete".
Works with Django 1.8.12 Firefox, Chrome.
To avoid duplicating anything of the original file, you can actually override the template while extending it (docs). So create your own template/admin/base_site.html
:
{% extends "admin/base_site.html" %} {% load static %} {% block extrahead %} <link rel="shortcut icon" href="{% static 'yourapp/img/favicon.ico' %}" /> {% endblock %}
Extend admin/base.html
in your template/admin/base_site.html
template and add the favicon link in extrahead block
{% extends "admin/base.html" %}
{% load staticfiles %}
...
{% block extrahead %}
<link rel="shortcut icon" href="{% static 'relative/path/to/favicon.ico' %}" />
{% endblock %}
Django version >= 2
Mind that the correct import, if using Django 2 or above, is:
{% load static %}`
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With