Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set favicon in django admin

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.

like image 818
kurtgn Avatar asked Jan 23 '16 05:01

kurtgn


People also ask

How to set favicon in Django admin?

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.

What is favicon ICO Django?

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.

What is a website favicon?

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.


3 Answers

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.

like image 94
user3526918 Avatar answered Sep 20 '22 22:09

user3526918


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 %} 
like image 20
th3hamm0r Avatar answered Sep 19 '22 22:09

th3hamm0r


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 %}`
like image 32
Antstud Avatar answered Sep 20 '22 22:09

Antstud