我需要为Django管理界面设置一个favicon。
最好的方法是全局设置,而不是覆盖所有应用程序的模板。
怎样才能做到最简洁呢?我尝试在Django文档中搜索相关内容,但没有找到任何信息。
最好的方法是全局设置,而不是覆盖所有应用程序的模板。
怎样才能做到最简洁呢?我尝试在Django文档中搜索相关内容,但没有找到任何信息。
为了避免重复原始文件中的任何内容,您实际上可以在扩展模板时覆盖模板(参见文档)。因此,请创建自己的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 %}
如果网站图标在 /app/static/img/favicon.ico
,则链接到这个文件的 {% block extrahead %}
中: /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 %}
在settings.py
中,确保你的应用在django.contrib.admin
之前列出。.pyc
文件来清除模板缓存以进行测试。$ find . -name \"*.pyc\" -delete".
适用于 Django 1.8.12,Firefox 和 Chrome。
template/admin/base_site.html
模板中,扩展admin/base.html
并在extrahead块中添加favicon链接。{% extends "admin/base.html" %}
{% load staticfiles %}
{% block extrahead %}
<link rel="shortcut icon" href="{% static 'relative/path/to/favicon.ico' %}" />
{% endblock %}
Django版本 >= 2
请注意,如果使用Django 2或更高版本,正确的导入方式是:
{% load static %}
将Django的base.html模板覆盖,并放置在admin目录下,例如my_app/templates/admin/base.html
。
在覆盖的模板中添加{% block extrahead %}
。
{% 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 %}
我发现最好的方法是将其放在URL中:
from django.shortcuts import redirect
urlpatterns = [
path('favicon.ico', lambda _ : redirect('static/icons/favicon.ico', permanent=True)),
]
这样做可以在所有页面上拥有一个网站图标,包括管理员和Django Rest Framework(我已经测试过了),并且使用链接标签在模板中设置网站图标能够持续生效。
django-project
|-core
| └-settings.py
|-my_app1
|-my_app2
|-static
| └-favicon.ico # Here
└-templates
└-admin
└-base.html # Here
BASE_DIR / 'templates'
设置为DIRS,并在STATICFILES_DIRS中将BASE_DIR / 'static/'
设置如下,这样Django就可以识别django-project
下的templates
和static
文件夹了。*我的回答解释了如何设置Django模板,我建议按照我的回答设置whitenoise以禁用浏览器缓存Django的静态文件。# "settings.py"
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
BASE_DIR / 'templates' # Here
],
...
},
]
...
STATIC_URL = 'static/'
STATICFILES_DIRS = [
BASE_DIR / 'static/' # Here
]
base.html
中添加如下代码:<link rel="icon" ...>
。{# "base.html" #}
<head>
<title>{% block title %}{% endblock %}</title>
<link rel="stylesheet" href="{% block stylesheet %}{% static "admin/css/base.css" %}{% endblock %}">
{# ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ Here ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓ #}
<link rel="icon" href="{% static 'favicon.ico' %}"/>
{# ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ Here ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ #}
{% block dark-mode-vars %}
<link rel="stylesheet" href="{% static "admin/css/dark_mode.css" %}">
<script src="{% static "admin/js/theme.js" %}" defer></script>
{% endblock %}
http://localhost:8000/...
http://localhost:8001/...
http://localhost:8002/...
http://localhost: :: /...
http://127.0.0.1:8000/...
http://127.0.0.1:8001/...
http://127.0.0.1:8002/...
http://127.0.0.1: :: /...