set favicon in django admin

前端 未结 4 1693
醉酒成梦
醉酒成梦 2021-02-18 14:08

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

相关标签:
4条回答
  • 2021-02-18 14:15

    Override the Django base.html template and put it under admin directory like my_app/templates/admin/base.html.

    Add {% block extrahead %} to the overriding template.

       {% extends 'admin/base.html' %}
        {% load staticfiles %}
        {% block javascripts %}
            {{ block.super }}
        <script type="text/javascript" src="{% static 'app/js/action.js' %}"></script>
    
        {% endblock %}
    
        {% block extrahead %}
            <link rel="shortcut icon" href="{% static 'app/img/favicon.ico'  %}" />
        {% endblock %}
        {% block stylesheets %}
    
            {{ block.super }}
        {% endblock %}
    
    0 讨论(0)
  • 2021-02-18 14:18

    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 %}
    
    0 讨论(0)
  • 2021-02-18 14:24

    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.

    0 讨论(0)
  • 2021-02-18 14:32

    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 %}
    
    0 讨论(0)
提交回复
热议问题