使用Heroku主持已经制作的Django网站

时间:2014-07-19 22:17:32

标签: python django sqlite postgresql heroku

我已经完成了用Django制作的网站,并希望将其付诸实施。我找到了Heroku,看到它对我的基本需求是免费的,并希望在那里举办。我的网站使用SQLite而不是PostgreSQL,所以我想知道如何使用它,因为它不支持sqlite。我在Heroku上找到了Django页面的入门,但我没有使用pip或virtualenv来设置它。

我应该采取哪些步骤来启动我的网站?仅供参考我正在使用最新的Django dev分支1.8和Python 3.4.1,它是一个个人网站。

这是我的settings.py文件

"""
Django settings for mysite project.

For more information on this file, see
https://docs.djangoproject.com/en/dev/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/dev/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/dev/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '0r58%!j00w2q1faj*57=d)*fv^=ai#-wgnakj91^--z5f(ohq1'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'mysite.urls'

WSGI_APPLICATION = 'mysite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/dev/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    }
}

# Internationalization
# https://docs.djangoproject.com/en/dev/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


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

STATIC_URL = '/static/'

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(BASE_DIR), "static", "templates"),
    )

if DEBUG:
    MEDIA_URL = '/media/'
    STATIC_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "static-only")
    MEDIA_ROOT = os.path.join(os.path.dirname(BASE_DIR), "static", "media")
    STATICFILES_DIRS = (
        os.path.join(os.path.dirname(BASE_DIR), "static", "static"),
        )

1 个答案:

答案 0 :(得分:0)

您需要一个包含所有依赖项的requirements.txt,因为heroku使用该文件为您提供所需的服务。

将postgreSQL附加组件添加到heroku项目后,heroku会为您生成一个DATABASE_URL环境变量。您可以根据heroku的教程在settings.py中解析它:

# Parse database configuration from $DATABASE_URL
import dj_database_url
DATABASES['default'] =  dj_database_url.config()

教程中还提供了基本的wsgi.py,您可以使用它。

from django.core.wsgi import get_wsgi_application
from dj_static import Cling

application = Cling(get_wsgi_application())

将应用程序提交到本地git存储库并将其推送到heroku远程存储库。如果您在控制台上执行此操作,您将获得有关部署的良好反馈。

基本上你需要遵循getting started tutorial。跳过虚拟环境的东西只会使生成需求文件更复杂,但它是可能的。