0

我的应用程序在我的本地正常运行,我可以运行

python manage.py collectstatic --noinput

没有错误。但是在构建过程结束时推送到 Heroku 时,出现以下错误:

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

所以我的构建被拒绝了。我查看了OSError: [Errno 2] No such file or directory: '/tmp/MakeCalls/Static'但是当我删除我STATICFILES_DIRS的 时,应用程序将部署,但我的所有静态资产都会出现 404 错误。我完全按照 Heroku上的说明进行操作,但它似乎不起作用。我的文件的相关部分settings.py如下:

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 = ['*']

和我的wsgi.py文件:

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)

这一切似乎在我的本地工作正常,当我运行heroku local web. 知道什么可能导致收集静态错误吗?

4

1 回答 1

1

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........

于 2017-02-07T07:22:19.970 回答