Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django heroku static dir

Tags:

django

heroku

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 [email protected]:[...]
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to '[email protected]:[...]'

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 ^^'

like image 391
heathen90 Avatar asked May 30 '12 22:05

heathen90


People also ask

Does Heroku support static files?

You should store them externally on a service like S3 - while Heroku can serve static files, it's not designed to.


1 Answers

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/'),
)
like image 69
K Z Avatar answered Sep 21 '22 12:09

K Z