36

I have a project (myapp) in heroku but I can't get the static files to work properly. I was following this blog post.

My Procfile looks like this:

web: python myapp/manage.py collectstatic --noinput; bin/gunicorn_django --workers=4 --bind=0.0.0.0:$PORT myapp/settings.py

settings.py:

...

STATIC_ROOT = os.path.join(PROJECT_PATH, 'staticfiles')
STATIC_URL = '/static/'
ADMIN_MEDIA_PREFIX = '/static/admin/'

STATICFILES_DIRS = (
    # I have the static folder inside my app and not inside the project
    os.path.join(PROJECT_PATH, 'cesar/static'),
)

...

When restarting using heroku restart this is what the heroku logs shows:

...
Copying ...

114 static files copied to '/app/myapp/staticfiles'.
...

But when I do heroku run ls -l myapp/ I can't see the staticfiles folder:

-rw------- 1 u5605 5605    0 Jan 28 16:53 __init__.py
drwx------ 4 u5605 5605 4096 Jan 28 16:53 cesar
-rw------- 1 u5605 5605  503 Jan 28 16:53 manage.py
-rw------- 1 u5605 5605 6292 Jan 28 16:53 settings.py
drwx------ 2 u5605 5605 4096 Jan 28 16:53 templates
-rw------- 1 u5605 5605  257 Jan 28 16:53 urls.py
-rw------- 1 u5605 5605  286 Jan 28 16:53 views.py

What am I missing or doing wrong?

César
  • 9,939
  • 6
  • 53
  • 74
  • 2
    I recently wrote an elaborate explanation on serving static files on S3 from an Heroku app, check it out - http://balzerg.blogspot.co.il/2012/09/staticfiles-on-heroku-with-django.html – idanzalz Sep 09 '12 at 09:05

2 Answers2

39

I found a solution. This was my initial myapp/urls.py:

from django.conf.urls.defaults import patterns, include, url
from django.contrib import admin
from django.conf import settings


admin.autodiscover()

urlpatterns = patterns('',
    url(r'^$', include('myapp.cesar.urls')),
    url(r'^admin/', include(admin.site.urls)),
)

I added these lines to the end of the original myapp/urls.py file:

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

Now it's working fine. I hope this helps someone else too

B Robster
  • 40,605
  • 21
  • 89
  • 122
César
  • 9,939
  • 6
  • 53
  • 74
  • I tried doing this, but I hit this error and my app crashes. Any ideas? `2012-03-23T21:55:57+00:00 app[web.1]: ImportError: Could not import settings 'appName/settings.py' (Is it on sys.path?): Import by filename is not supported.` – Aswath Krishnan Mar 23 '12 at 21:57
  • 1
    @Aswath try `from django.conf import settings` – Francis Yaconiello Aug 03 '12 at 13:10
  • http://stackoverflow.com/questions/10308985/django-on-heroku-broken-admin-static-files – Dmitry Jan 16 '13 at 08:30
  • 1
    For me, using Django 1.5.1, ['django.contrib.staticfiles.views'](https://docs.djangoproject.com/en/1.5/ref/contrib/staticfiles/#django.contrib.staticfiles.views.serve) worked whereas 'django.views.static.serve' did not. – snakesNbronies Jun 16 '13 at 05:23
0

Probably you should manually create empty STATIC_ROOT folder specified in you settings before running './manage.py collectstatic'.

machaku
  • 1,176
  • 8
  • 7