作为django Web应用程序的Graphite为所有静态资源返回404

Ale*_*lev 5 python django graphite

免责声明:我知道这里有很多404关于静态资源的问题django,但不幸的是它没有任何帮助:(

问题:正如标题中所述,我得到了404graphiteweb应用中所有静态资源的响应.

以下是一些相关的配置部分:

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',
)
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

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 *
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

目录布局:

/opt/graphite/
    /webapp/
        /graphite/
        /content/ <-- static files I want placed right here
Run Code Online (Sandbox Code Playgroud)

/opt/graphite 实际上是另一个文件夹的符号链接,如果这很重要.

我这样的网址有404错误:

http://my_host:9999/content/js/...
http://my_host:9999/content/css/...
Run Code Online (Sandbox Code Playgroud)

我也试过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.
Run Code Online (Sandbox Code Playgroud)

Django以下列方式启动:

/usr/bin/django-admin runserver --pythonpath /opt/graphite/webapp --settings graphite.settings 0.0.0.0:9999 &
Run Code Online (Sandbox Code Playgroud)

任何帮助将受到高度赞赏.

UPD:我已升级django到1.5并改变了一些路径:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "content"),
) 

STATIC_ROOT = ''
STATIC_URL = 'static'
Run Code Online (Sandbox Code Playgroud)

之后,collectstatic命令终于工作并将所有文件复制到/opt/graphite/staticdir.我决定尝试DEBUG = True奇迹发生 - 最后我得到了我想要的所有静态文件,但是当我将它恢复为默认设置时,404再次.

Ale*_*lev 2

正如我在问题中所写,设置DEBUG = True实际上解决了问题。这是因为django如果在开发模式下启动,则不会处理静态文件:Why does DEBUG=False 设置使我的 django 静态文件访问失败?

因此,开发模式服务器的解决方案包括:

  1. 升级django至1.5;
  2. 设置STATIC_ROOT''(适用于 Graphite);
  3. 将静态文件的真实操作系统文件系统路径添加到STATICFILES_DIRS
  4. 跑步python manage.py collectstatic --pythonpath=/opt/graphite/webapp