17

I'm new to heroku and I tried a simple django app without css.

But I just added a css file in my app and when i do this:

git push heroku master

The static file collection fails :

[...]
-----> Collecting static files
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 232, in execute
    output = self.handle(*args, **options)
  File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/management/base.py", line 371, in handle
    return self.handle_noargs(**options)
  File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 163, in handle_noargs
    collected = self.collect()
  File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 104, in collect
    for path, storage in finder.list(self.ignore_patterns):
  File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/contrib/staticfiles/finders.py", line 105, in list
    for path in utils.get_files(storage, ignore_patterns):
  File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/contrib/staticfiles/utils.py", line 25, in get_files
    directories, files = storage.listdir(location)
  File "/tmp/build_2unndirli15s7/.heroku/venv/lib/python2.7/site-packages/django/core/files/storage.py", line 235, in listdir
    for entry in os.listdir(path):
OSError: [Errno 2] No such file or directory: '/home/kevin/web/django/appheroku/blogapp/static'
 !     Heroku push rejected, failed to compile Python/django app

To git@heroku.com:[...]
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@heroku.com:[...]'

Here is my settings.py:

[...]
STATIC_ROOT = '/home/kevin/web/django/appheroku/staticfiles'

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    '/home/kevin/web/django/appheroku/blogapp/static',
)
[...]

The urls.py:

 [...]
 if settings.DEBUG:
   urlpatterns += staticfiles_urlpatterns()

Procfile :

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

It seems even the '/home/kevin/web/django/appheroku/blogapp/static' directory exists, it is not detected and I can't figure out why. :(

Please help me ^^

EDIT:

Now my settings.py looks like this :

import os
PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
PROJECT_DIR = os.path.join(PROJECT_ROOT,'../blogapp')
[...]
STATIC_ROOT = os.path.join(PROJECT_ROOT,'staticfiles/')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(PROJECT_DIR,'static/'),
)
[...]

And now the static file collection step seems to work well:

 -----> Collecting static files
        74 static files copied.

There was another problem : in my procfile, 'appheroku/manage.py' , 'bin/gunicorn_django' and 'appheroku/monblog/settings.py' were not found. I fixed it and now my procfile looks like this :

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

The app doesn't crash anymore but there is still no css. Here is the log:

'2012-05-31T17:35:16+00:00 heroku[router]: GET xxxx-xxxx-number.herokuapp.com/ dyno=web.1 queue=0 wait=0ms service=85ms status=200 bytes=1054
2012-05-31T17:35:17+00:00 heroku[router]: GET xxxx-xxxx-number.herokuapp.com/static/css/style.css dyno=web.1 queue=0 wait=0ms service=5ms status=404 bytes=2088
2012-05-31T17:35:17+00:00 heroku[router]: GET xxxx-xxxx-number.herokuapp.com/static/js/jquery-1.7.2.min.js dyno=web.1 queue=0 wait=0ms service=5ms status=404 bytes=2115'

EDIT3 :

Yes ! it works ^^ The problem was in my urls.py. I wrote :

[...]
if not settings.DEBUG:
   urlpatterns += staticfiles_urlpatterns()

instead of

 [...]
 if settings.DEBUG:
   urlpatterns += staticfiles_urlpatterns() 

The error was not in my message.. Yes, it was really difficult to help me. (Sorry) I feel so stupid ^^'

heathen90
  • 215
  • 3
  • 7

3 Answers3

16

The problem is the absolute path you are using for STATIC_ROOT isn't found in Heroku server.

To resolve it, consider the following approach.

In your settings.py:

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
STATIC_ROOT= os.path.join(PROJECT_DIR,'staticfiles/')
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT,'static/'),
)
K Z
  • 29,661
  • 8
  • 73
  • 78
  • Now I use this approach with some modification as explained in my edit. (because my static dir ('static/') is inside my app dir ('blogapp/') ). It resolves my problem (even now I'm with another one -_-) – heathen90 May 31 '12 at 09:58
  • Is `blogapp/` in the parent directory of this settings file? It seems more likely that `blogapp,` is in the same directory as the settings file which means you should just join `blogapp/` instead of `../blogapp`. – K Z May 31 '12 at 10:12
  • appheroku/ contains the monblog/ directory, the blogapp/ directory and the staticfiles directory. The blogapp/ directory contains the models.py file, the views.py file, the admin.py,.. (etc) and the static/ dir. The monblog/ directory contains the settings.py, the urls.py, etc... – heathen90 May 31 '12 at 10:29
3

You're on your way. What's happening is that Heroku is trying to use your STATICFILES_DIRS setting (/home/kevin/web/django/appheroku/blogapp/static) that exists locally on your machine but won't doesn't exist on the Heroku server.

A simple solution is to remove that line from the staticfiles_dir variable so you have:

STATICFILES_DIRS = ()

Then your default STATICFILES_FINDERS will kick in and you should be good to go to run your app both locally as well as deployed on Heroku.

eliotk
  • 76
  • 3
  • Thanks, I understand now. Your solution make disappear the "No such file or dir" error, but now there is an error like this: "Traceback (most recent call last): OSError: [Errno 30] Read-only file system:'/home/kevin' – heathen90 May 31 '12 at 09:26
  • So that new error should be fixed with Kay Zhu's recommendation. Regarding no CSS being shown, what are the errors in the heroku log when you try to go to a css page directly? You can tail the log with "heroku logs --tail" on the command line to get a better handle on what the hang up is – eliotk May 31 '12 at 15:45
  • GET xxx-xxx-number.herokuapp.com/static/css/style.css dyno=web.1 queue=0 wait=0ms service=6ms status=404 bytes=2088 – heathen90 May 31 '12 at 17:44
  • Finally ,it works. Thank you again, I was about to give up and your message made me think again to the problem. =) – heathen90 May 31 '12 at 18:08
  • Thanks for thoses answer. I did have the sames troubles ... How difficult it is just to add static files online !!! Sometime after spending hours I'm telling myself I should stick back to php – Stéphane Oct 19 '12 at 08:22
  • Thanks a lot. Sometime after trying for hours to upload a static file online I'm wondering my is that so complicated nowadays ... – Stéphane Oct 19 '12 at 08:23
  • This is the best answer. Let the finders locate the static folders. – Seth Sep 17 '15 at 21:07
1

For anyone who's coming in like me:

Like @eliotk implies,

PROJECT_ROOT = os.path.abspath(os.path.dirname(__file__))
STATIC_ROOT= os.path.join(PROJECT_DIR,'staticfiles/')
STATICFILES_DIRS = ()

This is the config you want to avoid FileNotFoundError: [Errno 2] No such file or directory: errors or OSError: [Errno 30] Read-only file system: errors.

Tychovk
  • 13
  • 3