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属性存在多余空格,导致路径解析错误。 - 修复步骤:
- 在base.html的
<head>或<body>中添加styles.css的正确引入,注意去掉路径内的空格:<link rel="stylesheet" href="{% static 'feedback/styles.css' %}"> - 修正所有静态文件链接的空格问题,比如将
" {% static 'feedback/field.css' %}"改为"{% static 'feedback/field.css' %}"。 - 确认
styles.css放置在static/feedback/目录下,且Django静态文件配置正常(INSTALLED_APPS包含django.contrib.staticfiles,settings.py中STATIC_URL = '/static/')。 - (可选)给当前页码添加选中样式,修改模板中的分页逻辑:
<li class="page-num{% if page_obj.number == p %} page-num-selected{% endif %}"> <a href="?page={{p}}">{{p}}</a> </li>
- 在base.html的
2. favicon.ico 404错误修复
- 问题原因:浏览器默认请求根目录下的
favicon.ico,但项目中未正确放置或引用该文件。 - 修复步骤:
- 将
favicon.ico放入static/feedback/目录。 - 在base.html的
<head>标签中添加favicon引用:<link rel="icon" href="{% static 'feedback/favicon.ico' %}" type="image/x-icon"> - 开发环境下确保Django静态文件服务正常;生产环境需执行
python manage.py collectstatic收集静态文件到指定目录。
- 将
内容的提问来源于stack exchange,提问作者odoacer22
相关产品推荐
相关产品推荐

