将 django 项目部署到 Heroku 时出现问题

Problem this deploy django project to Heroku

我尝试将 django 应用程序部署到 heroku 并出现错误:

remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Building on the Heroku-20 stack
remote: -----> Using buildpack: heroku/python
remote: -----> App not compatible with buildpack: https://buildpack-registry.s3.amazonaws.com/buildpacks/heroku/python.tgz
remote:        More info: https://devcenter.heroku.com/articles/buildpacks#detection-failure
remote: 
remote:  !     Push failed
remote: Verifying deploy...
remote: 
remote: !       Push rejected to tandt-online-webchat.
remote: 
To https://git.heroku.com/tandt-online-webchat.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: не удалось отправить некоторые ссылки в «https://git.heroku.com/tandt-online-webchat.git»

我做了所有需要做的事情: git 添加 . git commit -m "releze heroku vers 1" 当我 git push heroku master 我得到这个错误

我有所有需要的文件:Procfile 和 requirements.txt 在 Procfile 中,我有:web: gunicorn web_chat.wsgi 和 requirements.txt 我输入 pip freeze,然后我确定我有 gunicorn 她。

我做错了什么?

一个常见的错误是您拼错了 requirements.txt 文件。另一个可能发生的错误是您的提交出错了。为此,在本地重新初始化 git 存储库。然后确保添加了 requirements.txt 文件。你可以用

检查这个
git status

如果还是不行请告诉我。

remote: -----> $ python manage.py collectstatic --noinput
remote:        /tmp/build_61db1e8d/staticfiles
remote:        Post-processing 'css/base.css' failed!
remote:        Traceback (most recent call last):
remote:          File "/tmp/build_61db1e8d/manage.py", line 22, in <module>
remote:            main()
remote:          File "/tmp/build_61db1e8d/manage.py", line 18, in main
remote:            execute_from_command_line(sys.argv)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
remote:            utility.execute()
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 395, in execute
remote:            self.fetch_command(subcommand).run_from_argv(self.argv)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 330, in run_from_argv
remote:            self.execute(*args, **cmd_options)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 371, in execute
remote:            output = self.handle(*args, **options)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 194, in handle
remote:            collected = self.collect()
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 138, in collect
remote:            raise processed
remote:        whitenoise.storage.MissingFileError: The file 'css/web_chat_imgs/фон_д_к.png' could not be found with <whitenoise.storage.CompressedManifestStaticFilesStorage object at 0x7f16d93bc3d0>.
remote:        The CSS file 'css/base.css' references a file which could not be found:
remote:          css/web_chat_imgs/фон_д_к.png
remote:        Please check the URL references in this CSS file, particularly any
remote:        relative paths which might be pointing to the wrong location.
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:
remote: 
remote:           $ heroku config:set DISABLE_COLLECTSTATIC=1
remote: 
remote:        https://devcenter.heroku.com/articles/django-assets
remote:  !     Push rejected, failed to compile Python app.
remote: 
remote:  !     Push failed
remote: Verifying deploy...
remote: 
remote: !       Push rejected to tandt-online-webchat.
remote: 
To https://git.heroku.com/tandt-online-webchat.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: не удалось отправить некоторые ссылки в «https://git.heroku.com/tandt-online-webchat.git»

谢谢你,但我真的不完全理解你。这是我的 setings.py 文件的结尾,这里有什么问题吗?

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_URL = '/static/'
# STATIC_ROOT = BASE_DIR/ 'static'

STATICFILES_DIRS = [
    BASE_DIR/ 'asets',
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'

django_heroku.settings(locals())

是的,这是很多人面临的另一个问题。在您的 settings.py 文件中,

STATIC_ROOT = os.path.join(BASE_DIR, "static")

这允许 Heroku 将静态文件存储在其 git 存储库的 'static' 文件夹中。您的设置文件夹中必须已经有 static url。否则:

STATIC_URL = '/static/

此外,如果您有任何其他问题,请尝试编辑您的原始问题,而不是问一个新问题,因为这有时会过度扩展页面。

啊,我明白问题了……我想。所以 heroku 不会自动提供静态文件,你必须在你的 settings.py 中添加 whitenoise。我将把它的代码放在这里:

pip install whitenoise

然后,将 whitenoise 添加到您的中间件 类

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

然后,将其添加到 settings.py 文件的底部。所有这些信息都可以在这里找到:https://devcenter.heroku.com/articles/django-assets

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

如果所有这些都不起作用,请再次阅读您的错误。它说您正在引用一个不存在的文件。您可能正在尝试加载或访问根本不存在的文件。检查您的文件结构以确保您没有这样做。

非常感谢,伙计,但我开始思考命运本身不会让我将这个应用程序部署到 heroku ️️

remote:  !     Push rejected, failed to compile Python app.
remote: 
remote:  !     Push failed
remote:  !
remote:  ! ## Warning - The same version of this code has already been built: eee95ca439ed1b8c61ed275fd5d07488a2ff53d9
remote:  !
remote:  ! We have detected that you have triggered a build from source code with version eee95ca439ed1b8c61ed275fd5d07488a2ff53d9
remote:  ! at least twice. One common cause of this behavior is attempting to deploy code from a different branch.
remote:  !
remote:  ! If you are developing on a branch and deploying via git you must run:
remote:  !
remote:  !     git push heroku <branchname>:main
remote:  !
remote:  ! This article goes into details on the behavior:
remote:  !   https://devcenter.heroku.com/articles/duplicate-build-version
remote: 
remote: Verifying deploy...
remote: 
remote: !       Push rejected to tandt-online-webchat.
remote: 
To https://git.heroku.com/tandt-online-webchat.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: не удалось отправить некоторые ссылки в «https://git.heroku.com/tandt-online-webchat.git»

我完成了您之前录制的所有内容,但出现新错误

从 heroku.com 中删除应用程序,然后再次重复整个过程。这次应该可以成功了。