【问题标题】:Graphite as django web application returns 404 for all static resourcesGraphite 作为 django Web 应用程序为所有静态资源返回 404
【发布时间】:2014-10-22 10:42:27
【问题描述】:

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

问题:如标题所述,我收到了404graphite 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 再次出现。

【问题讨论】:

    标签: python django graphite


    【解决方案1】:

    首先,您似乎正在尝试将最新的 Graphite 与旧版本的 Django 一起使用,对吗?

    石墨的主分支需要 Django >= 1.4,这将为您提供 collectstatic 命令。

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

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

    【讨论】:

    • 我已将django 升级到1.5,现在collectstatic 可用。您首先编写的命令告诉我ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting,但是当我从STATICFILES_DIRS 中删除硬编码路径时,它已经完成且没有错误,并带有大量消息,例如Copying '/usr/lib/python2.6/site-packages/django/contrib/admin/static/admin/img/gis/move_vertex_on.png',但我仍然得到404 :(
    • 是的,这意味着设置中的路径设置不正确。首先,仔细检查您的石墨版本。如果您使用的是 0.9.12 版本,则该版本不使用 collectstatic 或 django 1.5。顺便说一句,您应该只编辑 local_settings.py,而不是直接编辑 settings.py。
    • Graphite 版本高于 0.10,取自 master 分支的 HEAD。我做了一些更改 - STATIC_ROOT 现在是 '',STATICFILES_DIRS 现在只是 join(WEBAPP_DIR, 'content'), 但 404 仍然在这里 :(
    • 另外,设置DEBUG = True 似乎会让django 给我我的文件。
    • 对于 0.10,不再有“内容”目录,而是称为“静态”。 404 是因为它在 /opt/graphite/webapp/static 中寻找文件。您应该重置您的 settings.py 并在静态目录中重新运行 collectstatic。
    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2018-08-26
      • 2018-12-07
      • 2011-07-25
      • 1970-01-01
      • 2017-04-27
      • 2016-04-03
      • 1970-01-01
      • 2019-05-23
      • 1970-01-01
      相关资源
      最近更新 更多