【问题标题】:How to launch your already made Django App on Heroku如何在 Heroku 上启动您已经制作的 Django 应用程序
【发布时间】:2012-08-31 17:50:42
【问题描述】:

我已经制作了一个在我的服务器上运行的 django 应用程序。我现在想使用 heroku 在网络上启动它,但是我找到的所有教程都让你开始一个全新的项目。我不知道该怎么做才能简单地更新我已经存在的 django 项目以使用 heroku。

我的文件现在是这样组织的:

in hcp:
       crunchWeb:
                 crunchWeb: files = _init_.py ; settings.py ; urls.py ; wsgi.py
                 crunchApp: files = _init_.py ; admin.py ; models.py ; views.py etc...
                 manage.py
                 sqlite3.db

       env: folders= bin ; helloflask ; include ; lib #all of these were created automatically 
       templates:  all my .html files

我想知道 heroku 教程 (https://devcenter.heroku.com/articles/django#using-a-different-wsgi-server) 中的哪些命令我仍然需要执行,哪些可以跳过.

我还想知道执行所有命令时我需要在哪个文件夹中

谢谢!

2012-09-06T21:44:52+00:00 app[web.1]:   File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/__init__.py", line 34, in __getattr__
2012-09-06T21:44:52+00:00 app[web.1]:   File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/utils.py", line 92, in __getitem__
2012-09-06T21:44:52+00:00 app[web.1]:     return getattr(connections[DEFAULT_DB_ALIAS], item)
2012-09-06T21:44:52+00:00 app[web.1]:     backend = load_backend(db['ENGINE'])
2012-09-06T21:44:52+00:00 app[web.1]:   File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/utils.py", line 24, in load_backend
2012-09-06T21:44:52+00:00 app[web.1]:     return import_module('.base', backend_name)
2012-09-06T21:44:52+00:00 app[web.1]:   File "/app/.heroku/venv/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
2012-09-06T21:44:52+00:00 app[web.1]:     __import__(name)
2012-09-06T21:44:52+00:00 app[web.1]:   File "/app/.heroku/venv/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 31, in <module>
2012-09-06T21:44:52+00:00 app[web.1]:     raise ImproperlyConfigured("Error loading either pysqlite2 or sqlite3 modules (tried in that order): %s" % exc)
2012-09-06T21:44:52+00:00 app[web.1]: django.core.exceptions.ImproperlyConfigured: Error loading either pysqlite2 or sqlite3 modules (tried in that order): No module named _sqlite3
2012-09-06T21:44:54+00:00 heroku[web.1]: Process exited with status 1
2012-09-06T21:44:54+00:00 heroku[web.1]: State changed from starting to crashed
2012-09-06T21:44:54+00:00 heroku[web.1]: State changed from crashed to starting
2012-09-06T21:44:58+00:00 heroku[web.1]: Starting process with command `python ./manage.py runserver 0.0.0.0:57395 --noreload`
2012-09-06T21:44:59+00:00 app[web.1]:   File "./manage.py", line 10, in <module>

settings.py

 # Django settings for crunchWeb project.
import dj_database_url

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
# ('Your Name', 'your_email@example.com'),
)

MANAGERS = ADMINS

DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}

# {
#     'default': {
#         'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
#         'NAME': '/Users/Santi/hcp/crunchWeb/sqlite3.db',                      # Or path to database file if using sqlite3.
#         'USER': '',                      # Not used with sqlite3.
#         'PASSWORD': '',                  # Not used with sqlite3.
#         'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
#         'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
#     }
# }

【问题讨论】:

    标签: python django heroku


    【解决方案1】:

    直接从指示https://devcenter.heroku.com/articles/django,再次,如果您阅读指示并遵循它们,您将执行:

    $pip install Django psycopg2 dj-database-url
    

    数据库设置

    接下来,配置应用程序以使用 Heroku 的 Postgres 数据库。已安装的 dj-database-url 模块将自动从 env 中完成所有操作。

    将以下内容添加到您的 settings.py:

    import dj_database_url
    DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
    

    您可以在 settings.py 的末尾添加这些行以继续在本地使用 sql lite,并且仅在 heroku 上使用 postgres。

    settings.py
    ------------------------
    import dj_database_url
    import os
    if os.getcwd() == "/app":
        DATABASES = {'default': dj_database_url.config(default='postgres://localhost')}
    

    【讨论】:

      【解决方案2】:

      您已链接的heroku/django deployment tutorial 仍然是您的最佳选择。我刚刚按照该教程部署了一个现有应用程序,并跳过了我已经完成的步骤,例如:

      virtualenv venv --distribute
      django-admin.py startapp...
      manage.py syncdb...
      git init
      

      基本上,如果您已经为您现有的应用程序干了这个!

      我在您上面的文件夹结构中没有看到 requirements.txt,所以请确保您

      pip freeze > requirements.txt 
      

      为了减少破坏我现有代码的可能性,我做了以下操作

      # create a branch for testing heroku
      git checkout -b heroku_test
      
      #... follow the heroku tutorial
      
      # add my changes
      git add .
      git commit -m "Heroku deployment steps"
      
      # add the heroku remote
      git remote add heroku [address]
      git push heroku heroku_test:master
      

      push 语句的最后一部分heroku_test:master 让您将本地分支heroku_test 推送到远程分支master。 (否则 heroku 将忽略提交,因为它不在 master 分支上)。

      【讨论】:

      • 我应该在哪个文件夹中创建 requirements.txt?
      • 在您的 django 项目的顶层,例如stackoverflow.com/questions/7974902/…
      • @will-hart 我想我快到了。但是,在我执行“heroku create”和“git push heroku master”然后通过执行“heroku ps”检查它是否正在运行后,我得到了错误:=== web:python ./manage.py runserver 0.0.0.0:$PORT --noreloadweb.1: crashed for 2m
      • 当您git push heroku master 时检查日志 - 他们应该会告诉您部署是否失败以及原因
      猜你喜欢
      • 1970-01-01
      • 2015-02-14
      • 2019-10-11
      • 2011-07-17
      • 2022-06-24
      • 2014-04-05
      • 2015-02-07
      • 2012-12-04
      相关资源
      最近更新 更多