简体   繁体   中英

Heroku error using collect static with Django 1.10

My app runs normally on my local and I can run

python manage.py collectstatic --noinput

without error. But when pushing to Heroku at the end of the build process I get the following error:

$ python manage.py collectstatic --noinput
    Traceback (most recent call last):
      File "manage.py", line 22, in <module>
        execute_from_command_line(sys.argv)
      File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 367, in execute_from_command_line
        utility.execute()
      File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/__init__.py", line 359, in execute
        self.fetch_command(subcommand).run_from_argv(self.argv)
      File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 294, in run_from_argv
        self.execute(*args, **cmd_options)
      File "/app/.heroku/python/lib/python2.7/site-packages/django/core/management/base.py", line 345, in execute
        output = self.handle(*args, **options)
      File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 193, in handle
        collected = self.collect()
      File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 115, in collect
        for path, storage in finder.list(self.ignore_patterns):
      File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/finders.py", line 112, in list
        for path in utils.get_files(storage, ignore_patterns):
      File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/utils.py", line 28, in get_files
        directories, files = storage.listdir(location)
      File "/app/.heroku/python/lib/python2.7/site-packages/django/core/files/storage.py", line 399, in listdir
        for entry in os.listdir(path):
    OSError: [Errno 2] No such file or directory: '/tmp/build_732715d9b29ba88f9eb56ca3d7e722de/MY_REAL_APP_NAME/static'

and so my build is rejected. I looked at OSError: [Errno 2] No such file or directory: '/tmp/MakeCalls/Static' but when I remove my STATICFILES_DIRS , the app will deploy but all my static assets get a 404 error. I'm following the instructions on Heroku exactly , but it doesn't seem to work. Relevant parts of my settings.py file is as follows:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)
STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'
ALLOWED_HOSTS = ['*']

and my wsgi.py file:

import os
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "fortuno.settings")
application = get_wsgi_application()
application = DjangoWhiteNoise(application)

This all seems to work ok on my local, and when I run heroku local web . Any idea what might be causing the collect static error?

STATIC FILE HANDLING IN DJANGO

in settings.py

import os
def root(folder):
    return os.path.join(os.path.abspath(os.path.dirname(__file__)), '..',folder)

create the static/media folder inside the project root

MEDIA_ROOT = root('media')
MEDIA_URL = '/media/'
STATIC_ROOT = root('staticstorage')
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    root('static'),
)

project root urls.py (project/project/urls.py)[django version 1.10 please reffer if you are not using this version]

from django.conf import settings
from django.conf.urls.static import static
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

make run python manage.py collectstatic in your local machine , if it's created staticstorage directory inside your project , it's done .... go ahead with deploy........

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM