【问题标题】:Push failed to Heroku - Python Django推送到 Heroku 失败 - Python Django
【发布时间】:2021-07-09 20:35:09
【问题描述】:

我试图在 heroku 上部署我的 django 应用程序,我没有创建虚拟环境,这是我第一次这样做。当我尝试推送到 heroku 时,安装所有软件包后出现错误-:

-----> $ python manage.py collectstatic --noinput
   Traceback (most recent call last):
     File "/tmp/build_b0d1f9a6/manage.py", line 22, in <module>
       main()
     File "/tmp/build_b0d1f9a6/manage.py", line 18, in main
       execute_from_command_line(sys.argv)
     File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
       utility.execute()
     File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 413, in execute
       self.fetch_command(subcommand).run_from_argv(self.argv)
     File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 354, in run_from_argv
       self.execute(*args, **cmd_options)
     File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 398, in execute
       output = self.handle(*args, **options)
     File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 187, in handle
       collected = self.collect()
     File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 114, in collect
       handler(path, prefixed_path, storage)
     File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 338, in copy_file
       if not self.delete_file(path, prefixed_path, source_storage):
     File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 248, in delete_file
       if self.storage.exists(prefixed_path):
     File "/app/.heroku/python/lib/python3.9/site-packages/django/core/files/storage.py", line 318, in exists
       return os.path.exists(self.path(name))
     File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/staticfiles/storage.py", line 38, in path
       raise ImproperlyConfigured("You're using the staticfiles app "
   django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
 !   Error while running '$ python manage.py collectstatic --noinput'.
     See traceback above for details.
     You may need to update application code to resolve this error.
     Or, you can disable collectstatic for this application:
       $ heroku config:set DISABLE_COLLECTSTATIC=1
     https://devcenter.heroku.com/articles/django-assets
 !   Push rejected, failed to compile Python app.
 !   Push failed

我不明白该怎么做,我是 heroku 的新手,git。

这是我的目录的外观,感谢任何帮助。

【问题讨论】:

    标签: django heroku


    【解决方案1】:

    我认为您没有在设置中添加 static_root...

    让我帮你, 确保您已将其添加到您的项目 urls.py 文件中

    from django.conf import settings
    from django.conf.urls.static import static
    
    urlpatterns = [
        # ... the rest of your URLconf goes here ...
    ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
    

    & 在你的 settings.py 中你必须添加这个,所以 collect static 会为这个域加载你的静态文件。 例如。

    STATIC_URL = '/static/'
    STATIC_ROOT = "/var/www/example.com/static/"
    

    然后尝试运行收集静态命令

    如果您想了解有关此静态文件的更多信息,可以查看此文档:

    https://docs.djangoproject.com/en/3.1/howto/static-files/

    https://docs.djangoproject.com/en/3.1/ref/settings/#std:setting-STATIC_ROOT

    【讨论】:

    • 所以基本上我需要将我的静态文件上传到任何云存储上,并给出一个链接?
    • 不!您必须将这些添加到您的 settings.py 文件中,然后运行命令:python manage.py collectstatic。然后它将起作用。我认为问题在于,heroku 无法检测到您的静态文件,因此我们必须提供这些静态文件的链接。通过做 static_root 你会告诉heroku在哪里可以找到那些静态文件。您可以从文档中了解静态文件。我已经提供了链接看看他们
    猜你喜欢
    • 1970-01-01
    • 2016-12-03
    • 2020-01-01
    • 2021-05-07
    • 2021-12-30
    相关资源
    最近更新 更多