django静态文件加载错误

报错信息如下:

"GET /blog/ HTTP/1.1" 200 3367
"GET /static/blog/css/style.css HTTP/1.1" 404 1667
"GET /static/blog/css/pygments/github.css HTTP/1.1" 404 1697

mysite/settings.py里面设置如下:

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'blog/static')

# STATICFILES_DIRS = (
#     os.path.join(BASE_DIR, 'blog/static'),
# )

blog/templates/blog/base.html里面设置如下:

{% load staticfiles %}
<link rel="stylesheet" href="{% static "blog/css/style.css" %}" />
<link rel="stylesheet" href="{% static "blog/css/pygments/github.css" %}" />

这是目录树

├── blog
│   ├── static
│   │   └── blog
│   │       └── css
│   │           ├── pygments
│   │           │   └── gighub.css
│   │           └── sytle.css
│   ├── templates
│   │   ├── base.html
│   │   └── blog
│   │       ├── comment.html
│   │       ├── detail.html
│   │       ├── index.html
│   │       └── pagination.html
├── manage.py
└── mysite
    ├── settings.py
    ├── settings.pyc

问题描述:

看了官方文档和Stackoverflow以及SegementFault上的很多解决方案,按理说我的INSRALLED_APPS里面有staticfiles这个app,我的static文件又是直接放在blog这个app下面,django应该能够自动识别出来,而且我又分别单独指定了STATIC_ROOTSTATICFILES_DIRS,但是不管怎么样sytle.css和github.css都报上面的那个404错误,请问有什么好的解决方案?


参考资料:
Google

官方文档

阅读 7.1k
2 个回答

1、官方文档有这么一句, 你看看你的Debug是不是True

During development, if you use django.contrib.staticfiles, this will be done automatically by runserver when DEBUG is set to True (see django.contrib.staticfiles.views.serve()).

2、static_root是给python manage.py collectstatic用的,这个目录是给app以外的共同Static目录使用的,不要与app自己目录下的static重名。我觉的你可以把这个配置去掉,或者指向另一个目录,比如project/static
3、如果你指定了static_root,可以运行python manage.py collectstatic,然后看一看,在static_root下是否正确归集了你的app下的静态文件,针对性的查一查
4、协助定位静态文件的还有这个命令可以协助你排查问题

python manage.py findstatic /blog/css/style.css --verbosity 2

一个小小的spell error。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题