【问题标题】:heroku django collectstatic errorheroku django collectstatic 错误
【发布时间】:2015-07-17 16:34:36
【问题描述】:

我正在尝试在 heroku 上运行收集我的静态文件,所以我运行了命令:

heroku run python manage.py collectstatic --noinput

我的设置文件是:

BASE_DIR = os.path.dirname(os.path.abspath(__file__))   
STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
# define the directory which all upload files will be saved
PROJECT_ROOT = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
MEDIA_ROOT = os.path.join(PROJECT_ROOT, 'MyApp/media/')
MEDIA_URL = '/MyApp/media/'

我收到以下错误消息:

OSError: [Errno 2] No such file or directory: '/app/MyApp/static'

这是什么意思?

为什么我应该在“/app/MyApp”中有一个“静态”文件?不应该是

STATIC_ROOT?

完整的追溯:

Running `python manage.py collectstatic --noinput` attached to terminal... up, r

un.9966

Traceback (most recent call last):
  File "manage.py", line 11, in <module>
      execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python2.7/sitepackages/django/core/management/_
_init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python2.7/sitepackages/django/core/management/_
_init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python2.7/sitepackages/django/core/management/base.py",
line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File     "/app/.heroku/python/lib/python2.7/sitepackages/django/core/management/base.py",     line 338, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python2.7/sitepackages/django/core/management/base.py",     line 533, in handle
    return self.handle_noargs(**options)
  File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 168, in handle_noargs
collected = self.collect()
  File "/app/.heroku/python/lib/python2.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 98, in collect
for path, storage in finder.list(self.ignore_patterns):
  File "/app/.heroku/python/lib/python2.7/sitepackages/django/contrib/staticfiles/finders.py", line 111, in list
for path in utils.get_files(storage, ignore_patterns):
      File "/app/.heroku/python/lib/python2.7/sitepackages/django/contrib/staticfiles/utils.py", line 27, in get_files
directories, files = storage.listdir(location)
      File "/app/.heroku/python/lib/python2.7/sitepackages/django/core/files/storage.py",     line 270, in listdir
for entry in os.listdir(path):
OSError: [Errno 2] No such file or directory: '/app/CTFdj/static'

编辑: OSError: [Errno 2] 没有这样的文件或目录:'/app/CTFdj/static' =>>

OSError: [Errno 2] 没有这样的文件或目录:'/app/MyApp/static'

【问题讨论】:

  • 请显示完整的回溯。
  • 您的STATIC_ROOT 错误。它应该是收集的静态目录的服务路径。
  • 什么意思?我的 STATIC_ROOT 声明不是意味着我的文件将在“staticfiles”目录下的主项目目录中提供吗?你能给我一个好的声明的例子吗?
  • @brad,我想我不应该说错,因为它也可能有效,但我不确定拥有 相对路径 的效果。文档说它应该是绝对路径

标签: django heroku


【解决方案1】:

我可能无法回答为什么它会查看/app/CTFdj/static,但一个可能的原因是您的STATIC_ROOT 可能配置错误。应该是the absolute path where the collected static will be copied to。看看我通常如何组织我的STATIC_ROOT。希望这会有所帮助。

【讨论】:

    【解决方案2】:

    我怀疑您收到该错误是因为您的 BASE_DIR 没有为您的项目结构正确设置。尝试在本地运行 python manage.py collectstatic --noinput 以查看是否遇到相同的错误。您还可以在settings.pyprint(BASE_DIR) 检查它是否指向正确的位置,以及static 文件夹是否实际上在该目录中。对于我的项目,我的设置文件有好几级,但static 目录位于项目的根目录,所以我的设置如下所示:

    BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) // base_dir/subdir/settings/settings.py
    STATICFILES_DIRS = (
        os.path.join(BASE_DIR, 'static'), // base_dir/static
    )
    

    另外,对于我的 Heroku 应用,我将 BASE_DIRstaticfiles 一起加入 STATIC_ROOT,如下所示:

    STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
    

    另外,我应该警告您,您不能像您目前使用MEDIA_ROOT 设置的那样将媒体文件直接上传到您的 Heroku 应用程序。 Heroku 使用 ephemeral filesystem 在测功机停止或重新启动后丢弃您写入的文件。您需要使用 Amazon S3 等第三方服务来上传用户提交的媒体文件。 django-storages 应用程序可以帮助进行集成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-05-13
      • 2017-05-30
      • 1970-01-01
      • 2014-03-14
      • 1970-01-01
      • 2016-12-08
      • 2020-09-14
      • 2021-10-13
      相关资源
      最近更新 更多