我只想把favicon。ico放到我的staticfiles目录中,然后让它显示在我的应用程序中。
我怎样才能做到呢?
我已经把favicon.ico文件放在我的staticfiles目录中,但它没有显示,我在日志中看到了这一点:
127.0.0.1 - - [21/Feb/2014 10:10:53] "GET /favicon.ico HTTP/1.1" 404 -
如果我去http://localhost:8000/static/favicon.ico,我可以看到图标。
我只想把favicon。ico放到我的staticfiles目录中,然后让它显示在我的应用程序中。
我怎样才能做到呢?
我已经把favicon.ico文件放在我的staticfiles目录中,但它没有显示,我在日志中看到了这一点:
127.0.0.1 - - [21/Feb/2014 10:10:53] "GET /favicon.ico HTTP/1.1" 404 -
如果我去http://localhost:8000/static/favicon.ico,我可以看到图标。
如果你有一个基本或头部模板,包括在任何地方,为什么不包括favicon与基本HTML?
<link rel="shortcut icon" type="image/png" href="{% static 'favicon.ico' %}"/>
一个轻量级的技巧是在你的urls.py文件中做一个重定向,例如添加一个这样的视图:
from django.views.generic.base import RedirectView
favicon_view = RedirectView.as_view(url='/static/favicon.ico', permanent=True)
urlpatterns = [
...
re_path(r'^favicon\.ico$', favicon_view),
...
]
当你真的没有其他静态内容要托管时,这是一个让favicons工作的简单技巧。
如果你允许的话
Alias /favicon.ico /var/www/aktel/workspace1/PyBot/PyBot/static/favicon.ico
为虚拟主机添加别名。(在apache配置文件中)类似于robots.txt
Alias /robots.txt /var/www/---your path ---/PyBot/robots.txt
在模板文件中
{% load static %}
然后在<head>标签内
<link rel="shortcut icon" href="{% static 'favicon.ico' %}">
这假设您已经在settings.py中适当配置了静态文件。
注意:旧版本的Django使用load staticfiles,而不是load static。
通用解决方案
你可以像在其他框架中一样在Django中显示favicon:只需要使用纯HTML。
将以下代码添加到HTML模板的头部。 如果favicon在整个应用程序中是相同的,最好是到您的基本HTML模板。
<link rel="shortcut icon" href="{% static 'favicon/favicon.png' %}"/>
前面的代码假设:
在静态文件夹中有一个名为“favicon”的文件夹 favicon文件名为favicon。png 您已经正确地设置了设置变量STATIC_URL
你可以在维基百科https://en.wikipedia.org/wiki/Favicon的这篇文章中找到关于文件格式支持和如何使用favicons的有用信息。 我可以推荐使用。png格式的浏览器兼容性。
编辑: 正如一条评论所言, “不要忘记在模板文件顶部添加{% load staticfiles %} !”
<link rel="shortcut icon" href="{% static 'favicon/favicon.ico' %}"/>
只需要在你的基本文件中添加它,就像第一个答案但ico扩展,并将其添加到静态文件夹
最好的解决方案是重写Django base.html模板。在admin目录下创建另一个base.html模板。如果不存在,首先创建一个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 %}
我在django 2.1.1中尝试了以下设置
base.html
<head>
{% load static %}
<link rel="shortcut icon" type="image/png" href="{% static 'images/favicon.ico' %}"/>
</head>
settings.py
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_URL = '/static/'` <br>`.............
项目目录结构
现场观看
最佳实践:
与你所想的相反,favicon可以是任何大小和任何图像类型。点击这个链接了解详情。
不给你的图标添加链接会降低页面加载速度。
在django项目中,假设你的favicon的路径是:
myapp/static/icons/favicon.png
在你的django模板中(最好是在基本模板中),添加这一行到页面的头部:
<link rel="shortcut icon" href="{% static 'icons/favicon.png' %}">
注意:
我们假设,静态设置在settings.py中配置得很好。
在你的settings.py中添加一个根目录staticfiles:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
创建/静态/图片/ ico。位于
添加图标到你的模板(base.html):
{% load static %}
<link rel="shortcut icon" type="image/png" href="{% static 'images/favicon.ico' %}"/>
并在urls.py中创建一个url重定向,因为浏览器会在/favicon.ico中查找favicon
from django.contrib.staticfiles.storage import staticfiles_storage
from django.views.generic.base import RedirectView
urlpatterns = [
...
path('favicon.ico', RedirectView.as_view(url=staticfiles_storage.url('images/favicon.ico')))
]
复制你的图标: / yourappname mainapp(例:核心)/静态/ mainapp(例:核心)/ img
然后转到mainapp模板(例如:base.html) 复制这个,在{% load static %}后面,因为你必须先加载静态。
<link href="{% static 'core/img/favi_x.png' %}" rel="shortcut icon" type="image/png" />
<link rel="shortcut icon" type="image/png" href="{% static 'favicon/sample.png' %}" />
也可以运行:python manage.py collectstatic
在寻求帮助时偶然发现的。我试图在我的Django项目中实现favicon,它没有显示——想添加到对话中。
当我试图在我的Django项目中实现favicon时,我将“favicon.ico”文件重命名为“my_filename.ico”——图像不会显示。重命名为“favicon.ico”后解决了这个问题并显示了图形。下面是解决我的问题的代码:
<link rel="shortcut icon" type="image/png" href="{% static 'img/favicon.ico' %}" />
现在(2020年), 你可以在html文件中添加一个基本标签。
<head>
<base href="https://www.example.com/static/">
</head>
第一个
将favicon.ico上传到应用程序的静态路径,或者在settings.py中通过STATICFILES_DIRS配置的路径
第二个
在app基础模板文件中:
{% load static %}
<link rel="shortcut icon" type="image/png" href="{% static 'favicon.ico' %}"/>
你可以让应用程序使用不同的favicon.ico文件。
除了
在项目/ urls . py
from django.templatetags.static import static # Not from django.conf.urls.static
from django.views.generic.base import RedirectView
将此路径添加到urlpatterns基本位置
path('favicon.ico', RedirectView.as_view(url=static('favicon.ico'))),
这可以让安装的app(比如admin,你不应该改变模板)和你忘记修改模板的app,也显示默认的favicon.ico