6

I have a django app that I am successfully deplying to heroku. When I dry-run the collectstatic command locally everything works fine.

python manage.py collectstatic --dry-run --noinput
....
Pretending to copy '/Users/hari/.virtualenvs/bsc2/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/admin/ordering.js'
Pretending to copy '/Users/hari/.virtualenvs/bsc2/lib/python2.7/site-packages/django/contrib/admin/static/admin/js/admin/RelatedObjectLookups.js'

71 static files copied.

Despite this ..my django admin staticfiles do not get used and I get a bare-bones django admin site on heroku with Debug set to False.

If I set Debug to True I get a "rich" admin site on heroku. With Debug set to True or False "git push heroku master " command terminal output does not have anything about collecting staticfiles.

I tried the example "helloworld" application that uses gunicorn from Heroku and that did display the "collecting static" messages.I also tried inserting this code snippet into my urls.py. But that too does not help.

from django.conf import settings

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

Next, I tried adding the following to my heroku config

heroku config:add DISABLE_COLLECTSTATIC=0

But that too did not show my django admin site with all the styles.

Finally I tried switching to gunicorn with my Procfile and that also did not show the admin styles. Only setting Debug=True works to show my admin styles.

I tried this with Django 1.4.2 and 1.5.1 on Heroku and neither is showing me a "normal" admin site. Is there any way out to have my admin files on heroku without going the S3 route.

harijay
  • 11,303
  • 12
  • 38
  • 52

4 Answers4

11

command terminal output does not have anything about collecting staticfiles.

Looking at heroku-buildpack-python:bin/steps/collectstatic it seems that it tries to do a collectstatic --dry-run --noinput and pipes it's output to /dev/null before displaying the -----> Collecting static files message. This means that if there is an error there that is not present on your local box, you will never see the error on heroku : it will silently fail. (The best kind of failure ;)

have you tried running a one-off worker to test out the collectstatic command and see if it's a problem in their environment?

heroku run python manage.py collectstatic --dry-run --noinput

If this fails, it will give you an error or traceback to look into and further diagnose the issue.

Thomas
  • 11,757
  • 4
  • 41
  • 57
  • Thanks Thomas, running the command "heroku run python manage.py collectstatic --dry-run --noinput" gives me an error "OSError: [Errno 2] No such file or directory: '/app/bsc2/assets'" . – harijay Apr 07 '13 at 11:55
  • Is there supposed to be a folder by that name? what happens when you remove that directory on your local machine? do you get the same error? – Thomas Apr 07 '13 at 12:02
  • 1
    Thanks Thomas, running the command "heroku run python manage.py collectstatic --dry-run --noinput" gives me an error "OSError: [Errno 2] No such file or directory: '/app/bsc2/assets'" . I did have an empty assets directory that I did not realize git does not checkout ( because it was empty). Once I touched a Readme.txt "touch bsc2/bsc2/assets/Readme.txt" in that directory and committed and pushed..heroku could see the directory. Now I get "Collecting Static files" msg, and importantly rich admin site despite Debug set to False...Sorry the comment policy prevented edit before you commented – harijay Apr 07 '13 at 12:23
  • 1
    Ah! I've been bitten by this git behaviour before. Glad it all worked out for you! – Thomas Apr 07 '13 at 12:40
2

Check this out Django and Static Assets. It seems to be updated recently and you can serve static files nicely using this dj-static package.

chhantyal
  • 11,874
  • 7
  • 51
  • 77
2

Try these three things:

  • Create this Heroku config variable: DJANGO_SETTINGS_MODULE with a value of myapp.settings.prod--or as appropriate for your Heroku settings file
  • Use Whitenoise as described in the Heroku docs: https://devcenter.heroku.com/articles/django-assets
  • Check it in and redeploy your dyno: git push heroku master

I found I was missing the first item, the DJANGO_SETTINGS_MODULE" e.g., Command line collectstatic would work but that didn't matter b/c it was an ephemeral dyno

rprasad
  • 366
  • 3
  • 8
1

throwing it out there, since dumb mistakes happen:

I spent much time trying to figure out why my module wasn't found on heroku when it was working fine locally, only to realize it was ignored by an entry into .slugignore

MrE
  • 19,584
  • 12
  • 87
  • 105