我只想把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,我可以看到图标。


当前回答

        <link rel="shortcut icon" type="image/png" href="{% static 'favicon/sample.png' %}" />

也可以运行:python manage.py collectstatic

其他回答

        <link rel="shortcut icon" type="image/png" href="{% static 'favicon/sample.png' %}" />

也可以运行:python manage.py collectstatic

如果你有一个基本或头部模板,包括在任何地方,为什么不包括favicon与基本HTML?

<link rel="shortcut icon" type="image/png" href="{% static 'favicon.ico' %}"/>

现在(2020年), 你可以在html文件中添加一个基本标签。

<head>
<base href="https://www.example.com/static/"> 
</head>
<link rel="shortcut icon" href="{% static 'favicon/favicon.ico' %}"/>

只需要在你的基本文件中添加它,就像第一个答案但ico扩展,并将其添加到静态文件夹

有时重新启动服务器会有所帮助。

停止服务器,然后重新运行命令:python manage.py runserver 现在应该加载CSS文件了。