You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django项目导航栏无法识别静态文件,出现favicon.ico 404错误

问题:Django分页导航栏CSS样式不生效 + favicon.ico 404错误

错误日志

Not Found: /favicon.ico
[04/Mar/2024 17:57:46] "GET /favicon.ico HTTP/1.1" 404 3581
[04/Mar/2024 18:17:46] "GET /list?page=1 HTTP/1.1" 200 1374

相关代码

views.py

class ListFeedBack(ListView):
    template_name = 'feedback/list_feedback.html'
    model=Feedback
    paginate_by = 2

list_feedback.html

{% extends 'feedback/base.html' %}

{% block content %}
{% load static %}
<h2>Все отзывы</h2>
<table>
    <tr>
        <th>Номер</th>
        <th>Имя</th>
        <th>Фамилия</th>
        <th>Отзывы</th>
        <th>Рейтинг</th>
    </tr>
    {% for i in object_list %}
    <tr>
        <th><a href="{{i.get_url}}">{{i.id}}</a></th>
        <th>{{i.name}}</th>
        <th>{{i.surname}}</th>
        <th>{{i.feedback}}</th>
        <th>{{i.rating}}</th>
    </tr>
    {% endfor %}
</table>
{% endblock %}

{% block navbar %}
   <nav class="list-pages">
       <ul>
           {% for p in paginator.page_range %}
           <li class="page-num">
               <a href="?page={{p}}">{{p}}</a>
           </li>
           {% endfor %}
       </ul>
   </nav>
{% endblock %}

base.html

<!DOCTYPE html>
{% load static %}

<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>  {% block title %} {% endblock %}  </title>
    <link rel="stylesheet" href=" {% static 'feedback/field.css' %}">
</head>
<body>
   {% block content %}
   {% endblock %}
   <link rel="stylesheet" href=" {% static 'feedback/table.css' %}">
   {% block navbar %}
   {% endblock %}
</body>
</html>

urls.py

from django.contrib import admin
from django.urls import path
from . import views

urlpatterns = [
    path('done', views.DoneView.as_view()),
    path('', views.FeedBackView.as_view()),
    path('list', views.ListFeedBack.as_view()),
    path('<int:id_feedback>', views.UpdateFeedbackView.as_view(), name='update_feed'),
    path('detail/<int:pk>', views.DetailFeedBack.as_view(),name='feedback_details'),
    path('update/<int:pk>', views.FeedBackViewUpdate.as_view(),name='feedback_details'),
]

styles.css(导航栏样式)

.list-pages {
    text-align: center;
    margin: 0 0 20px 0;
}
.list-pages ul {
    margin: 20px 0 0 0;
    padding: 0;
    list-style: none;
}
.list-pages ul li {
    display: inline-block;
    margin: 0 20px 0 0;
}
.list-pages a {
    color: #000;
    font-size: 24px;
    text-decoration: none;
}
.list-pages .page-num, .page-num-selected {
    display: inline-block;
    width: 60px;
    height: 44px;
    padding: 16px 0 0 0;
    border: 1px solid #d0d0d0;
    border-radius: 30px;
}
.list-pages .page-num:hover {
    box-shadow: 3px 3px 1px #d0d0d0;
}
.list-pages .page-num-selected {
    border: none;
    color: #000;
    font-size: 20px;
}
.list-pages .page-num-selected:hover {
    box-shadow: none;
}

用户说明:已尝试配置favicon路径但错误依旧,base.html包含空的navbar块。


解决方法

1. 分页导航栏CSS样式不生效修复

  • 问题原因:导航栏样式定义在styles.css中,但base.html未引入该文件;同时静态文件链接的href属性存在多余空格,导致路径解析错误。
  • 修复步骤:
    1. 在base.html的<head>或<body>中添加styles.css的正确引入,注意去掉路径内的空格:
      <link rel="stylesheet" href="{% static 'feedback/styles.css' %}">
      
    2. 修正所有静态文件链接的空格问题,比如将" {% static 'feedback/field.css' %}"改为"{% static 'feedback/field.css' %}"。
    3. 确认styles.css放置在static/feedback/目录下,且Django静态文件配置正常(INSTALLED_APPS包含django.contrib.staticfiles,settings.py中STATIC_URL = '/static/')。
    4. (可选)给当前页码添加选中样式,修改模板中的分页逻辑:
      <li class="page-num{% if page_obj.number == p %} page-num-selected{% endif %}">
          <a href="?page={{p}}">{{p}}</a>
      </li>
      

2. favicon.ico 404错误修复

  • 问题原因:浏览器默认请求根目录下的favicon.ico,但项目中未正确放置或引用该文件。
  • 修复步骤:
    1. 将favicon.ico放入static/feedback/目录。
    2. 在base.html的<head>标签中添加favicon引用:
      <link rel="icon" href="{% static 'feedback/favicon.ico' %}" type="image/x-icon">
      
    3. 开发环境下确保Django静态文件服务正常;生产环境需执行python manage.py collectstatic收集静态文件到指定目录。

内容的提问来源于stack exchange,提问作者odoacer22

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.28 16:32:00