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

时间:2014-10-22 10:42:27

标签: python django graphite

免责声明:我知道404中关于django的静态资源存在很多问题,但不幸的是,它们都没有帮助:(

问题:如标题所述,我对404网络应用中的所有静态资源都进行了graphite响应。

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

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 &

任何帮助都将受到高度赞赏。

UPD:我已将django升级为1.5并更改了一些路径:

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

STATIC_ROOT = ''
STATIC_URL = 'static'

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

2 个答案:

答案 0 :(得分:2)

首先,看起来你正在尝试使用最新的Graphite和旧版本的Django,这是正确的吗?

石墨的主分支需要Django&gt; = 1.4,它会给你collectstatic命令。

collectstatic的正确命令也需要pythonpath,因此它会选择正确的石墨设置 - python manage.py collectstatic --pythonpath=/opt/graphite/webapp

Collectstatic需要无误地运行,你应该看到/ opt / graphite / webapp / content中的文件。之后,如果您看到文件系统位置上存在的文件有404,那么您的webapp设置就会出现问题(例如,在没有pythonpath的情况下启动)。

答案 1 :(得分:2)

正如我在问题中所写,设置DEBUG = True实际上解决了问题。这是因为如果以开发模式启动,django不处理静态文件:Why does DEBUG=False setting make my django Static Files Access fail?

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

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