Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Graphite as django web application returns 404 for all static resources

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.

like image 554
Alexey Malev Avatar asked Sep 30 '22 13:09

Alexey Malev


2 Answers

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).

like image 170
danny Avatar answered Oct 08 '22 07:10

danny


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
like image 2
Alexey Malev Avatar answered Oct 08 '22 06:10

Alexey Malev