【问题标题】:Can't run django app with rest api on Heroku无法在 Heroku 上使用 rest api 运行 django 应用程序
【发布时间】:2017-11-05 02:50:19
【问题描述】:

我刚刚在“Heroku.com”上创建了一个帐户,并按照指南中的步骤创建了一个新的 python 项目。我在本地克隆了文件并向项目添加了一个新应用程序,并创建了一个 rest api。我将“rest_framework”添加到“settings.py”中,并且能够在本地运行该项目。当我尝试将更改推送回heroku时,它失败了,我收到了这条消息:

Counting objects: 6, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (6/6), done.
Writing objects: 100% (6/6), 632 bytes | 0 bytes/s, done.
Total 6 (delta 5), reused 0 (delta 0)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Python app detected
remote: -----> Installing requirements with pip
remote:
remote: Package django found!
remote: -----> $ python manage.py collectstatic --noinput
remote:        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 353, in 
execute_from_command_line
remote:            utility.execute()
remote:          File "/app/.heroku/python/lib/python3.6/site-
packages/django/core/management/__init__.py", line 327, in execute
remote:            django.setup()
remote:          File "/app/.heroku/python/lib/python3.6/site-
packages/django/__init__.py", line 18, in setup
remote:            apps.populate(settings.INSTALLED_APPS)
remote:          File "/app/.heroku/python/lib/python3.6/site-
packages/django/apps/registry.py", line 85, in populate
remote:            app_config = AppConfig.create(entry)
remote:          File "/app/.heroku/python/lib/python3.6/site-
packages/django/apps/config.py", line 90, in create
remote:            module = import_module(entry)
remote:          File 
"/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in 
import_module
remote:            return _bootstrap._gcd_import(name[level:], package, 
level)
remote:          File "<frozen importlib._bootstrap>", line 978, in 
_gcd_import
remote:          File "<frozen importlib._bootstrap>", line 961, in 
_find_and_load
remote:          File "<frozen importlib._bootstrap>", line 948, in 
_find_and_load_unlocked
remote:        ModuleNotFoundError: No module named 'rest_framework'
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 xplearner.
remote:
To https://git.heroku.com/xplearner.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/xplearner.git'

当我从“settings.py”中删除“rest_framework”时,我可以成功地将更改推送到“Heroku”,但我无法再在本地运行该应用程序,因为它在我的设置和相同情况下都找不到restframework ,当我尝试在 Heroku 上在线启动项目时出现此错误:

ModuleNotFoundError at /admin/
No module named 'rest_framework'

请问有人可以提出解决此问题的方法吗?我做错了什么?

【问题讨论】:

  • 您似乎忘记将rest_framework 添加到您的requirements.txt 文件中。你能给我们看看吗?
  • BTW 在 PyPI rest_framework 被列为 djangorestframework
  • 我没有将 restframework 添加到我的 requirements.txt 文件中。我刚刚做了,我还必须将迁移应用到我的 heroku 应用程序,它运行良好。非常感谢,我很感激。

标签: python django git heroku


【解决方案1】:

也许这是一个 python2/python3 问题。您可以将rest_framework 更改为djangorestframework如果您在本地使用带有此包的python3,您能否详细说明有关要求和heroku confs 的信息。

【讨论】:

  • 感谢您的快速回复。问题出在我的 requirements.txt 文件上。我没有向它添加restframework。谢谢,我很感激。
【解决方案2】:

对我来说,将它放在 requirements.txt 中是不够的。这是一个新应用程序的全新部署,我以前从未见过这样的事情,但我不得不将需求添加到 Pipfile。为此,我在我的虚拟环境中使用了pipenv install。然后我只是添加并提交了 Pipfile。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-05
    • 2018-01-20
    • 2015-11-30
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 2018-12-03
    • 2013-02-01
    相关资源
    最近更新 更多