【问题标题】:Static file issues with Heroku and DjangoHeroku 和 Django 的静态文件问题
【发布时间】:2018-09-10 16:54:55
【问题描述】:

我的网站运行良好,但上传到 Heroku 的图片开始出现问题。我在 Django 中使用 whitenoise,但决定将我的图像存储和静态文件移动到 AWS。我一直在学习本教程,并且能够将我的静态文件夹中的文件升级到 S3。

但是现在我在尝试部署到 Heroku 时遇到了问题。

请注意,我的 app 文件夹内有一个 static 文件夹,位于根目录内。

推送时出现此错误——其中包括一个 collectstatic

FileNotFoundError: [Errno 2] No such file or directory: '/tmp/build_f05a3a5f9e4b6ad44dfdf0b62dd16e9e/static'

我很确定它是 Heroku(?)的临时存储区,但我猜 /static 不应该放在最后。

我将在此处复制我的相关 settings.py 变量:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
DEFAULT_FILE_STORAGE = 'dealmazing.storage_backends.MediaStorage'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')

# Additional locations of static files
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

#This will make sure that the file URL does not have unnecessary parameters like your access key.
AWS_ACCESS_KEY_ID = config('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = config('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = config('AWS_STORAGE_BUCKET_NAME')
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=86400',
}
AWS_LOCATION = 'static'

# Static files (CSS, JavaScript, Images)
STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
ADMIN_MEDIA_PREFIX = STATIC_URL + 'admin/'

#media storage settings
MEDIA_URL = STATIC_URL + 'media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

如果需要,这里有完整的回溯:

 Traceback (most recent call last):
remote:          File "manage.py", line 10, in <module>
remote:            execute_from_command_line(sys.argv)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
remote:            utility.execute()
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 365, in execute
remote:            self.fetch_command(subcommand).run_from_argv(self.argv)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 288, in run_from_argv
remote:            self.execute(*args, **cmd_options)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/base.py", line 335, in execute
remote:            output = self.handle(*args, **options)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 189, in handle
remote:            collected = self.collect()
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 105, in collect
remote:            for path, storage in finder.list(self.ignore_patterns):
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/finders.py", line 125, in list
remote:            for path in utils.get_files(storage, ignore_patterns):
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/contrib/staticfiles/utils.py", line 28, in get_files
remote:            directories, files = storage.listdir(location)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/core/files/storage.py", line 313, in listdir
remote:            for entry in os.listdir(path):
remote:        FileNotFoundError: [Errno 2] No such file or directory: '/tmp/build_f05a3a5f9e4b6ad44dfdf0b62dd16e9e/static'
remote:
remote:  !     Error while running '$ python manage.py collectstatic --noinput'.
remote:        See traceback above for details.
remote:
remote:        You may need to update application code to resolve this error.
remote:        Or, you can disable collectstatic for this application:

【问题讨论】:

    标签: django heroku


    【解决方案1】:

    我认为问题在于您的 static 目录是空的,因此 git 不会跟踪它,因此当 Heroku 尝试构建您的项目时,该目录不存在并且您会收到该错误。

    如果您在 static 目录中添加一个名为 .keep 的空文件并将其添加到 git 中,则可以解决此问题。

    【讨论】:

    • 感谢您的评论——非常感谢。我认为可能与 Whitenoise 存在冲突——所以我删除了它并按照你的建议做了,一切都很好。感谢您的帮助。
    • 我的静态目录不为空还有其他建议吗?
    【解决方案2】:

    对于其他人和未来的我:https://github.com/not-kennethreitz/flango/issues/3 ;)

    STATIC_TMP = os.path.join(BASE_DIR, 'static')
    STATIC_URL = '/static/'
    
    os.makedirs(STATIC_TMP, exist_ok=True)
    os.makedirs(STATIC_ROOT, exist_ok=True)
    

    【讨论】:

      猜你喜欢
      • 2016-11-13
      • 1970-01-01
      • 1970-01-01
      • 2015-11-06
      • 2015-12-15
      • 2023-03-20
      • 2011-06-11
      • 2014-02-04
      • 1970-01-01
      相关资源
      最近更新 更多