警告未找到:Django 中的 /favicon.ico
我是 Python 和 Django 新手。在执行 runserver 后,当我尝试从登录页面登录时,我看到此错误消息,
$ python manage.py runserver
Running in development mode.
Running in development mode.
Running in development mode.
Running in development mode.
Validating models...
0 errors found
Django version 1.4b1, using settings 'platformsite.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
[21/Feb/2012 02:33:26] "GET /accounts/home/ HTTP/1.1" 200 10698
WARNING 2012-02-21 02:33:27,204 base 41333 4353703936 Not Found: /favicon.ico
[21/Feb/2012 02:33:30] "POST /accounts/home/ HTTP/1.1" 200 11098
WARNING 2012-02-21 02:33:30,581 base 41333 4362117120 Not Found: /favicon.ico
[21/Feb/2012 02:33:35] "POST /accounts/home/ HTTP/1.1" 200 10975
WARNING 2012-02-21 02:33:36,333 base 41333 4370530304 Not Found: /favicon.ico
[21/Feb/2012 02:33:57] "POST /accounts/home/ HTTP/1.1" 200 10975
WARNING 2012-02-21 02:33:57,670 base 41333 4349497344 Not Found: /favicon.ico
我使用的是 Python 2.7、Django 1.4 和 OS X 10.7 这个警告是什么意思?我该如何消除它?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
大多数浏览器会在网站域的根路径中查找是否存在名为 favicon.ico 的文件,该文件控制您可以在书签文件夹或浏览器地址栏中看到的网站图标。
如果您没有,那么它会返回“未找到”错误。
Most browsers look for the existence of an file called favicon.ico at the root path of your website domain, this controls the icon for the website you can see in your bookmarks folder or the address bar of your browser.
If you don't have one, then it's valid that it would return a Not Found error.
当您部署到 Apache 之类的系统时,您必须在配置文件中为您的网站图标添加别名。但是,在开发模式下运行 Django 时,urls.py 可以执行以下操作
:
When you deploy to something like Apache, you will have to alias your favicon in a config file. However, while running Django in development mode, the following works
urls.py:
几乎所有浏览器通常都会在网站域的根路径中查找名为
favicon.ico
的文件,该文件控制我们可以在选项卡、书签文件夹或浏览器的地址栏。当浏览器搜索
favicon.ico
并没有找到相同的内容时,它会返回 Not Found 错误。django.contrib.staticfiles.storage
提供staticfiles_storage
,它是项目配置的静态文件存储的实例。存储有一个 URL 方法,“[r]返回一个绝对 URL,Web 浏览器可以直接访问该文件的内容。”,这正是{% static %}
标记所使用的。因此,模板中的
{% static 'favicon.ico' %}
本质上相当于 Python 中的staticfiles_storage.url('favicon.ico')
。我们可以在下添加
标记到django的
base.html
。当我们在其他域中遇到错误代码为 404 的错误时,可以在
部分下添加 favicon 的链接,我们可以添加
,如下所示:
或者
不要忘记添加图标文件
favicon.ico
所需的位置,问题将会得到解决。favicon.ico
可以是公司的徽标或网站所基于的任何内容。Almost all Browsers usually look for the existence of a file called
favicon.ico
at the root path of the website domain, this controls the icon for the website that we can see in the tabs, bookmarks folder or the address bar of the browser.When the browser searches for
favicon.ico
and does not find the same, then it is valid that it would return a Not Found error.django.contrib.staticfiles.storage
providesstaticfiles_storage
, which is an instance of your project’s configured static file storage. Storages have a URL method that “[r]eturns an absolute URL where the file’s contents can be accessed directly by a Web browser.”, which is exactly what the{% static %}
tag uses.So
{% static 'favicon.ico' %}
in a template is essentially equivalent tostaticfiles_storage.url('favicon.ico')
in Python. We can add<link rel="icon" href="{% static 'base/icons/favicon.ico' %}">
under<head>
tag to thebase.html
of django.When we get such error in other domains with error code 404, the link for favicon can be added under
<head>
section we can add<link>
as follows:or
Do not forget to add the required location of the icon file
favicon.ico
, the issue will be resolved.The
favicon.ico
could be the logo of the company or anything the website is based upon.我遇到了同样的问题
favicon.ico
找不到。我将图像文件从 https://favicon.ico 转换为
favicon.ico
。 io/favicon-converter/。下载 zip 文件后,创建名为 favicon 的新文件夹,并将 zip 中的所有文件复制到 favicon 文件夹中。
之后,我只需复制 favicon 文件夹并粘贴到静态文件夹中。
我在
urls.py
文件(我们的settings.py
文件所在的文件夹)中更新了这行代码。也许我的错误得到解决。
希望这对您有帮助!
I was facing same problem
favicon.ico
not found.I converted my image file to
favicon.ico
from https://favicon.io/favicon-converter/.After downloading the zip file, then created new folder by name favicon and copy all the file inside zip or you can extract to favicon folder.
After that I simply copied the favicon folder and pasted inside the static folder.
I update this line of code in
urls.py
file, the folder in which oursettings.py
file exist.Probably my error get solved.
Hope this will help you!
您的浏览器正在寻找可以在地址栏中显示的图标。要么给它一个,要么忽略警告。
Your browser is looking for a favicon that it can display in the Location Bar. Either give it one, or ignore the warning.
您可以通过发送
static_path
设置来提供静态文件作为关键字参数。我们将从以下位置提供这些文件
/static/
URI(这可以通过static_url_prefix
设置),我们将提供/favicon.ico
和
/robots.txt
来自同一目录。的自定义子类StaticFileHandler
可以通过以下方式指定static_handler_class
设置。”“”
You can serve static files by sending the
static_path
settingas a keyword argument. We will serve those files from the
/static/
URI (this is configurable with thestatic_url_prefix
setting), and we will serve/favicon.ico
and
/robots.txt
from the same directory. A custom subclass ofStaticFileHandler
can be specified with thestatic_handler_class
setting."""
一个简单的方法是在 apache conf 文件中创建一个别名:
An easy way to do it is to create an alias in your apache conf file:
我正在构建一个 Web api,也遇到了这个问题。由于这只是一个演示项目,我在浏览器中抛出了一个虚拟响应以使错误消失。在主 urls.py 中,我在其上方添加了我的小假视图。显然,在生产中,您只需将该图标保留在静态根目录中,即您在服务器配置文件中设置的位置。
I was building a web api and ran into this as well. As it was just a demo project, I threw a dummy response at the browser to make the error go away. In the main
urls.py
, I added my little fake view above it. Obviously, in production, you would just keep that icon in the static root, the location of which you set in the server config file.如果您只想删除警告而不设置自己的 favicon,请将
如下所示。 *答案解释
添加到
base.html
中的我的回答解释了如何为 Django 设置 favicon 和 我的回答解释了如何为Django设置favicon行政:
If you just want to remove the warning without setting your own favicon, add
<link rel="icon" ...>
to<head></head>
inbase.html
as shown below. *The answer explains<link rel="icon" ...>
below and my answer explains how to set favicon for Django and my answer explains how to set favicon for Django Admin:路径('favicon.ico', RedirectView.as_view(url='/staticfiles/favicon.ico')) ->将其用于 django...它有效@tinyhare 写了 naa..谢谢
path('favicon.ico', RedirectView.as_view(url='/staticfiles/favicon.ico')) -> use this for django... it worked @tinyhare wrote naa..Thanks