【发布时间】:2014-10-22 10:42:27
【问题描述】:
免责声明:我知道404 有很多关于django 静态资源的问题,但不幸的是,这些问题都没有帮助:(
问题:如标题所述,我收到了404 对graphite web 应用程序中所有静态资源的响应。
以下是一些相关的配置部分:
app_settings.py:
INSTALLED_APPS = (
'graphite.metrics',
'graphite.render',
'graphite.browser',
'graphite.composer',
'graphite.account',
'graphite.dashboard',
'graphite.whitelist',
'graphite.events',
'graphite.url_shortener',
'django.contrib.auth',
'django.contrib.sessions',
'django.contrib.admin',
'django.contrib.contenttypes',
'django.contrib.staticfiles',
'tagging',
)
settings.py:
# Defaults for these are set after local_settings is imported
STATIC_ROOT = ''
STATIC_URL = ''
...
## Load our local_settings
try:
from graphite.local_settings import * # noqa
except ImportError:
print >> sys.stderr, "Could not import graphite.local_settings, using defaults!"
...
STATICFILES_DIRS = (
join(WEBAPP_DIR, 'content'),
"/opt/graphite/webapp/content/",
)
if not STATIC_ROOT:
STATIC_ROOT = join(GRAPHITE_ROOT, 'static')
...
# Handle URL prefix in static files handling
if URL_PREFIX and not STATIC_URL.startswith(URL_PREFIX):
STATIC_URL = '/{0}{1}'.format(URL_PREFIX.strip('/'), STATIC_URL)
local_settings.py:
STATIC_ROOT = '/opt/graphite/webapp/content/'
STATIC_URL = '/content/'
STATICFILES_DIRS = (
# os.path.join(BASE_DIR, "static"),
# os.path.join(BASE_DIR, "content"),
"/opt/graphite/webapp/content/",
)
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)
...
from graphite.app_settings import *
Django 版本:
Python 2.6.6 (r266:84292, Jan 22 2014, 09:42:36)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import django
>>> django.VERSION
(1, 4, 14, 'final', 0)
目录布局:
/opt/graphite/
/webapp/
/graphite/
/content/ <-- static files I want placed right here
/opt/graphite 实际上是指向另一个文件夹的符号链接,如果这很重要的话。
我收到这样的 url 404 错误:
http://my_host:9999/content/js/...
http://my_host:9999/content/css/...
我也试过collectstatic命令没有成功:
[root@myserver graphite]# pwd
/opt/graphite/webapp/graphite
[root@myserver graphite]# python manage.py collectstatic
Could not import graphite.local_settings, using defaults!
Unknown command: 'collectstatic'
Type 'manage.py help' for usage.
[root@myserver graphite]# django-admin collectstatic --noinput --settings=graphite.settings
Unknown command: 'collectstatic'
Type 'django-admin help' for usage.
[root@myserver graphite]# python manage.py collectstatic --noinput --settings=graphite.settings
Unknown command: 'collectstatic'
Type 'manage.py help' for usage.
Django的启动方式如下:
/usr/bin/django-admin runserver --pythonpath /opt/graphite/webapp --settings graphite.settings 0.0.0.0:9999 &
任何帮助将不胜感激。
UPD:我已将django 升级到 1.5 并更改了一些路径:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "content"),
)
STATIC_ROOT = ''
STATIC_URL = 'static'
之后,collectstatic 命令终于起作用并将所有文件复制到/opt/graphite/static 目录。我决定尝试DEBUG = True,奇迹发生了——最后我得到了我想要的所有静态文件,但是当我将其恢复为默认设置时,404 再次出现。
【问题讨论】: