Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django on Heroku -- static files not being found

I am running Django on Heroku. I can successfully run collectstatic, but when I go to the site it is obvious that Django is unable to find my static files. Here's a snippet from my settings -- I think it's mostly standard stuff:

STATIC_ROOT = ''

# URL prefix for static files.
# Example: "http://media.lawrence.com/static/"
STATIC_URL = '/static/'

PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))


# Additional locations of static files
STATICFILES_DIRS = (
    # Put strings here, like "/home/html/static" or "C:/www/django/static".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
    os.path.join(PROJECT_DIR, 'static'),
)

# List of finder classes that know how to find static files in
# various locations.
if CLAYS_ENV == 'dev':
    STATICFILES_FINDERS = (
        'django.contrib.staticfiles.finders.FileSystemFinder',
        'django.contrib.staticfiles.finders.AppDirectoriesFinder',
        'django.contrib.staticfiles.finders.DefaultStorageFinder',
    )

And in my case the CLAYS_ENV variable will be set to 'dev'. Any ideas on why Django can successfully run collectstatic, but then can't find the files afterwards?

like image 710
Clay Wardell Avatar asked Jul 17 '12 16:07

Clay Wardell


2 Answers

Collect static and serving static media are two different things. Collectstatic just puts your static media in your STATIC_DIRS

Serving your static files is a process of the server not the collectstatic command. Ive never deployed to heroku before but do you have a url to map your static assets to their location?

if not settings.DEBUG:
    urlpatterns += patterns('',
        (r'^static/(?P<path>.*)$', 'django.views.static.serve', {'document_root': settings.STATIC_ROOT}),
    )

Which server are you using? Usually you want the server to serve static media, though, instead of django. In apache this is done by setting up an Alias

like image 141
dm03514 Avatar answered Sep 19 '22 16:09

dm03514


It's recommended to serve static files through a CDN (like Amazon S3) when using Heroku. While you can still manage to serve those directly from Heroku, the same process attending dynamic requests is also serving static data, wasting processing time. Also, in case of media files, the use of a CDN is mandatory, since Heroku's file-system is "ephimeral": Every time you deploy new code, a new image of the Cedar stack is recreated from scratch and a new code checkout is made. Every file not Git-tracked created between deploys is lost.

like image 39
Armando Pérez Marqués Avatar answered Sep 22 '22 16:09

Armando Pérez Marqués