【问题标题】:Heroku error using collect static with Django 1.10Heroku 错误在 Django 1.10 中使用 collect static
【发布时间】:2017-06-24 05:44:01
【问题描述】:

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

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 exactly 上的说明进行操作,但它似乎不起作用。我的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 时。知道什么可能导致收集静态错误吗?

【问题讨论】:

  • 您是否在 Django 项目文件夹中创建了“静态”文件夹?如果没有,请使用空文件创建一个。查看 Heroku 文档中的示例存储库以及此空 file 的路径
  • 是的,我有。它充满了我的引导文件和图像。
  • 你能展示你的项目目录结构吗?

标签: python django heroku collectstatic


【解决方案1】:

DJANGO 中的静态文件处理

在settings.py中

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

在项目根目录中创建静态/媒体文件夹

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 1.10版本,如果你没有使用这个版本,请参考]

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)

在你的本地机器上运行python manage.py collectstatic,如果它在你的项目中创建了staticstorage目录,它就完成了......继续部署............

【讨论】:

    猜你喜欢
    • 2017-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-28
    • 2021-06-02
    • 2017-05-30
    • 1970-01-01
    • 2017-04-21
    相关资源
    最近更新 更多