警告未找到:Django 中的 /favicon.ico

发布于 2025-01-07 07:42:23 字数 1062 浏览 1 评论 0 原文

我是 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 这个警告是什么意思?我该如何消除它?

I'm new to Python and Django. I'm seeing this error message after I perform runserver, when trying to log in from my landing page,

$ 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

I'm on Python 2.7, Django 1.4, and OS X 10.7
What is this warning about and how do I get rid of it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

定格我的天空 2025-01-14 07:42:24

大多数浏览器会在网站域的根路径中查找是否存在名为 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.

海拔太高太耀眼 2025-01-14 07:42:24

当您部署到 Apache 之类的系统时,您必须在配置文件中为您的网站图标添加别名。但是,在开发模式下运行 Django 时,urls.py 可以执行以下操作

from django.views.generic import RedirectView
from django.conf.urls import url

url_patterns=[
    ...

    url(r'^favicon\.ico
,RedirectView.as_view(url='/static/images/favicon.ico')),
]

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:

from django.views.generic import RedirectView
from django.conf.urls import url

url_patterns=[
    ...

    url(r'^favicon\.ico
,RedirectView.as_view(url='/static/images/favicon.ico')),
]
薯片软お妹 2025-01-14 07:42:24

几乎所有浏览器通常都会在网站域的根路径中查找名为 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 的链接,我们可以添加 ,如下所示:

<link rel="icon" href="static/projectname/favicon.ico">

或者

<link rel="shortcut icon" type="image/x-icon" href="base/icons/favicon.png"/>

不要忘记添加图标文件 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 provides staticfiles_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 to staticfiles_storage.url('favicon.ico') in Python. We can add <link rel="icon" href="{% static 'base/icons/favicon.ico' %}"> under <head> tag to the base.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:

<link rel="icon" href="static/projectname/favicon.ico">

or

<link rel="shortcut icon" type="image/x-icon" href="base/icons/favicon.png"/>

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.

天涯离梦残月幽梦 2025-01-14 07:42:24
  • 我遇到了同样的问题 favicon.ico 找不到。

  • 我将图像文件从 https://favicon.ico 转换为 favicon.ico。 io/favicon-converter/

  • 下载 zip 文件后,创建名为 favicon 的新文件夹,并将 zip 中的所有文件复制到 favicon 文件夹中。

  • 之后,我只需复制 favicon 文件夹并粘贴到静态文件夹中。

  • 我在 urls.py 文件(我们的 settings.py 文件所在的文件夹)中更新了这行代码。

     来自 django.urls 导入路径
     从 。导入视图
     从 django.contrib.staticfiles.storage 导入 staticfiles_storage
     从 django.views.generic.base 导入 RedirectView
    
     url 模式 = [    
          路径('favicon.ico', RedirectView.as_view(url=staticfiles_storage.url('favicon/favicon.ico')))
     ]
    
  • 也许我的错误得到解决。

  • 希望这对您有帮助!

  • 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 our settings.py file exist.

     from django.urls import path
     from . import views
     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('favicon/favicon.ico')))
     ]
    
  • Probably my error get solved.

  • Hope this will help you!

木格 2025-01-14 07:42:24

您的浏览器正在寻找可以在地址栏中显示的图标。要么给它一个,要么忽略警告。

Your browser is looking for a favicon that it can display in the Location Bar. Either give it one, or ignore the warning.

谈场末日恋爱 2025-01-14 07:42:24

您可以通过发送 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 setting
as a keyword argument. We will serve those files from the
/static/ URI (this is configurable with the
static_url_prefix setting), and we will serve /favicon.ico
and /robots.txt from the same directory. A custom subclass of
StaticFileHandler can be specified with the
static_handler_class setting.
"""

俯瞰星空 2025-01-14 07:42:24

一个简单的方法是在 apache conf 文件中创建一个别名:

<VirtualHost *:443>
    ...
    Alias /favicon.ico /path/to/your/favicon.ico
    ...
</VirtualHost>

An easy way to do it is to create an alias in your apache conf file:

<VirtualHost *:443>
    ...
    Alias /favicon.ico /path/to/your/favicon.ico
    ...
</VirtualHost>
巴黎盛开的樱花 2025-01-14 07:42:24

我正在构建一个 Web api,也遇到了这个问题。由于这只是一个演示项目,我在浏览器中抛出了一个虚拟响应以使错误消失。在主 urls.py 中,我在其上方添加了我的小假视图。显然,在生产中,您只需将该图标保留在静态根目录中,即您在服务器配置文件中设置的位置。

from django.http import HttpResponse

def okay(request):
    return HttpResponse('pretend-binary-data-here', content_type='image/jpeg')

urlpatterns = [
    path('favicon.ico', okay),
    ...
] 

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.

from django.http import HttpResponse

def okay(request):
    return HttpResponse('pretend-binary-data-here', content_type='image/jpeg')

urlpatterns = [
    path('favicon.ico', okay),
    ...
] 
眼眸里的快感 2025-01-14 07:42:24

如果您只想删除警告而不设置自己的 favicon,请将 添加到 base.html 中的 如下所示。 *答案解释我的回答解释了如何为 Django 设置 favicon我的回答解释了如何为Django设置favicon行政:

{# "base.html" #}

<head>
...
<link rel="icon" href="data:,"> {# Here #}
...
</head>

If you just want to remove the warning without setting your own favicon, add <link rel="icon" ...> to <head></head> in base.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:

{# "base.html" #}

<head>
...
<link rel="icon" href="data:,"> {# Here #}
...
</head>
痴情换悲伤 2025-01-14 07:42:24

路径('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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文