5

DISCLAIMER: I know there's lots of questions here for 404 on static resources in django, but unfortunately none of it helps :(

Problem: as stated in title, I got 404 response for all static resources in graphite web app.

Here are some relevant config parts:

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 version:

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)

Directories layout:

/opt/graphite/
    /webapp/
        /graphite/
        /content/ <-- static files I want placed right here

/opt/graphite is actually a symlink to another folder, if that matters.

I got 404 errors for the urls like this:

http://my_host:9999/content/js/...
http://my_host:9999/content/css/...

I have also tried collectstatic command with no success:

[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 is started in the following way:

/usr/bin/django-admin runserver --pythonpath /opt/graphite/webapp --settings graphite.settings 0.0.0.0:9999 &

Any help will be high appreciated.

UPD: I have upgraded django to 1.5 and changed some paths:

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

STATIC_ROOT = ''
STATIC_URL = 'static'

After that, collectstatic command finally worked and copied all files to /opt/graphite/static dir. I decided to try DEBUG = True and a miracle happened - finally I got all static files I wanted, but when I reverted it to default settings, 404 again.

Alexey Malev
  • 6,408
  • 4
  • 34
  • 52

2 Answers2

2

Firstly, it looks like you are trying to use latest Graphite with an old version of Django, is that correct?

Master branch of graphite needs Django >= 1.4 which would give you the collectstatic command.

The correct command for collectstatic also needs the pythonpath so it picks up the correct graphite settings - python manage.py collectstatic --pythonpath=/opt/graphite/webapp.

Collectstatic needs to run without errors and you should see the files in /opt/graphite/webapp/content. After that if you see 404s for files that do exist on the filesystem location then there's something wrong with your webapp settings (started without pythonpath for example).

danny
  • 5,140
  • 1
  • 19
  • 31
  • I have upgraded `django` to 1.5 and now `collectstatic` is available. The command you wrote first told me `ImproperlyConfigured: The STATICFILES_DIRS setting should not contain the STATIC_ROOT setting`, but when I removed hardcoded paths from `STATICFILES_DIRS`, it has completed without errors with numerous messages like `Copying '/usr/lib/python2.6/site-packages/django/contrib/admin/static/admin/img/gis/move_vertex_on.png'`, but I still got 404 :( – Alexey Malev Oct 22 '14 at 12:31
  • Yup, that means the path is not setup correctly in settings. Firstly, double check your graphite version. If you're using the 0.9.12 release, that one does not use collectstatic or django 1.5. You should only be editing local_settings.py btw, not settings.py directly. – danny Oct 22 '14 at 12:35
  • Graphite version is something above 0.10, taken from HEAD of master branch. I have done some changes - `STATIC_ROOT` is now '', `STATICFILES_DIRS` is now `join(WEBAPP_DIR, 'content'),` only but 404 is still here :( – Alexey Malev Oct 22 '14 at 12:50
  • Also, it seems setting `DEBUG = True` somehow makes `django` to give me my files. – Alexey Malev Oct 22 '14 at 12:57
  • For 0.10 there is no 'content' directory anymore, it is called 'static' instead. The 404s are because it's looking for files in /opt/graphite/webapp/static. You should reset your settings.py and rerun collectstatic in the static directory. – danny Oct 22 '14 at 13:08
2

As I wrote in the question, setting DEBUG = True actually solves the problem. That is because django doesn't handle static files if started in dev mode: Why does DEBUG=False setting make my django Static Files Access fail?

So, the solution for the dev mode server includes:

  1. Upgrade django to 1.5;
  2. Set STATIC_ROOT to '' (works for Graphite);
  3. Add real OS filesystem path to static files to STATICFILES_DIRS;
  4. Run python manage.py collectstatic --pythonpath=/opt/graphite/webapp
Community
  • 1
  • 1
Alexey Malev
  • 6,408
  • 4
  • 34
  • 52