Graphite作为Django web应用返回404错误的所有静态资源
免责声明:我知道这里有很多关于 404
错误和静态资源在 django
中的问题,但不幸的是,没有一个能帮到我 :(
问题:正如标题所说,我在 graphite
网络应用中遇到了所有静态资源的 404
响应。
以下是一些相关的配置部分:
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
实际上是指向另一个文件夹的符号链接,如果这有关系的话。
我在访问以下网址时遇到了 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 &
任何帮助都将非常感激。
更新:我把 django
升级到了 1.5,并更改了一些路径:
STATICFILES_DIRS = (
os.path.join(BASE_DIR, "content"),
)
STATIC_ROOT = ''
STATIC_URL = 'static'
之后,collectstatic
命令终于成功了,所有文件都复制到了 /opt/graphite/static
目录。我决定尝试 DEBUG = True
,结果奇迹发生了 - 我终于得到了我想要的所有静态文件,但当我把它恢复到默认设置时,404
又回来了。
2 个回答
首先,看起来你是想用最新的Graphite,但你的Django版本太旧了,是这样吗?
Graphite的主分支需要Django版本在1.4及以上,这样你才能使用collectstatic这个命令。
要正确使用collectstatic命令,还需要指定pythonpath,这样才能找到正确的Graphite设置,命令是:python manage.py collectstatic --pythonpath=/opt/graphite/webapp
。
运行collectstatic时要确保没有错误,这样你就能在/opt/graphite/webapp/content文件夹里看到文件。如果之后你发现有些文件明明在文件系统里却显示404错误,那可能是你的webapp设置有问题,比如没有正确设置pythonpath。
正如我在问题中提到的,把 DEBUG = True
设置为真确实能解决这个问题。这是因为当 django
在开发模式下启动时,它不会处理静态文件。你可以查看这个链接了解更多原因:为什么将DEBUG设置为False会导致我的Django静态文件无法访问?
所以,在开发模式下服务器的解决方案包括:
- 把
django
升级到1.5版本; - 把
STATIC_ROOT
设置为空字符串''
(这对Graphite有效); - 在
STATICFILES_DIRS
中添加静态文件的真实操作系统文件路径; - 运行
python manage.py collectstatic --pythonpath=/opt/graphite/webapp
。