找不到 Heroku 上传静态文件夹

Heroku upload static folder not found

我试图重现教程 Django 应用程序并将其上传到 Heroku 服务器,但我无法解决静态文件的问题。

这里是 link github 上的所有文件: https://github.com/Rufus90/poll.git

当我尝试 运行 heroku 运行 python manage.py collectstatic --noinput

我收到这个错误:

Running python manage.py collectstatic --noinput on ⬢ hidden-plains-30510... up, run.1265 (Free)
Traceback (most recent call last):
  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 188, in handle
    collected = self.collect()
  File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 105, in collect
    for path, storage in finder.list(self.ignore_patterns):
  File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/staticfiles/finders.py", line 131, in list
    for path in utils.get_files(storage, ignore_patterns):
  File "/app/.heroku/python/lib/python3.7/site-packages/django/contrib/staticfiles/utils.py", line 23, in get_files
    directories, files = storage.listdir(location)
  File "/app/.heroku/python/lib/python3.7/site-packages/django/core/files/storage.py", line 315, in listdir
    for entry in os.scandir(path):
FileNotFoundError: [Errno 2] No such file or directory: '/app/static'

完成本教程后,我复制了整个存储库,将其重命名并更改了我认为需要的路径。我正在玩教程中的旧项目,但它完全搞砸了,根本停止工作,所以我坚持这个。 现在我什至不能用新的静态文件替换旧的静态文件或更改字体或任何东西,我不知道问题出在哪里。

有人可以解释一下我做错了什么吗?除了复制文件夹并重命名..

根据 heroku documentation,您需要在代码库中创建该目录 /app/static,以便在 运行 collectstatic 时可用。以下是文档中提到的内容:

Django won’t automatically create the target directory (STATIC_ROOT) that collectstatic uses, if it isn’t available. You may need to create this directory in your codebase, so it will be available when collectstatic is run. Git does not support empty file directories, so you will have to create a file inside that directory as well.

或者您可以在heroku中使用whitenoise。由于 django 在生产模式下不提供静态文件(当您在设置中设置 DEBUG=False 时),这里 whitenoise 变得很方便。它可以在生产模式下提供静态文件。将它集成到您​​的 django 项目中也很容易。

首先,使用pip install whitenoise安装它。

然后在settings.py中添加WhiteNoiseMiddleware

MIDDLEWARE_CLASSES = (
    # Simplified static file serving.
    # https://warehouse.python.org/project/whitenoise/
    'whitenoise.middleware.WhiteNoiseMiddleware',
    ...

最后,如果您希望为静态文件启用 GZIP 存储,请将以下行放入 settings.py 文件中:

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

可以找到有关白噪声的更多信息here