我一直试图在Heroku上设置我的Django应用程序几天,似乎已经用尽我的搜索可用于我的问题,至少据我所知 .

Problem Summary: 我能够将我的应用程序推送到Heroku,它似乎成功运行,因为Heroku确认已部署并且构建成功 . 但是当我尝试 heroku open 我的应用程序时,它返回服务器错误 .

以下是我遇到错误时的操作:

git add -A .
git commit -m "initial commit"
heroku create kronos-punch-clock
git push heroku master

以上似乎工作正常,没有任何错误 . 然后我继续说:

heroku ps:scale web=1

Heroku回复:

缩放dynos ...完成,现在运行web at 1:Free

heroku ps

Heroku回复:

gunicorn kronos_clock.wsgi:application --log-file - (1)web.1:up 2016/07/28 17:42:53 -0400(~20s ago)

然后我跑:

heroku open

唉,我得到了一个: Server Error(500) 消息 .

以下是我运行 heroku logs 的日志:

2016-07-28T21:41:23.094364 00:00 heroku [api]:通过zz@gmail.com启用Logplex 2016-07-28T21:41:23.094472 00:00 heroku [api]:发布v2由zz @ gmail创建.com 2016-07-28T21:42:42.109542 00:00 heroku [api]:发布v3由zz@gmail.com创建2016-07-28T21:42:42.109446 00:00 heroku [api]:附加数据库(@ref) :postgresql-cubed-19724)by zz@gmail.com 2016-07-28T21:42:42.660435 00:00 heroku [api]:通过zz@gmail.com扩展到web = 1 2016-07-28T21:42:42.661108 00:00 heroku [api]:发布v4由zz@gmail.com创建2016-07-28T21:42:42.661045 00:00 heroku [api]:部署4935f2a by zz@gmail.com 2016-07-28T21:42: 43.215886 00:00 heroku [slug-compiler]:Slug编译开始2016-07-28T21:42:43.215897 00:00 heroku [slug-compiler]:Slug编译完成2016-07-28T21:42:49.688459 00:00 heroku [ web.1]:使用命令gunicorn启动进程kronos_clock.wsgi:application --log-file - 2016-07-28T21:42:52.848547 00:00 app [web.1]:[2016-07-28 21:42: 52 0000] [3] [INFO]使用worker:sync 2016-07-28T21:42:52 . 848383 00:00 app [web.1]:[2016-07-28 21:42:52 0000] [3] [INFO]听力:http://0.0.0.0:20911(3)2016-07-28T21 :42:52.953366 00:00 app [web.1]:[2016-07-28 21:42:52 0000] [8] [INFO]启动 Worker 带pid:8 2016-07-28T21:42:52.856553 00: 00 app [web.1]:[2016-07-28 21:42:52 0000] [7] [INFO]启动 Worker 带pid:7 2016-07-28T21:42:52.833181 00:00 app [web.1 ]:[2016-07-28 21:42:52 0000] [3] [INFO]开始gunicorn 19.6.0 2016-07-28T21:42:53.445129 00:00 heroku [web.1]:状态从开始变为up 2016-07-28T21:43:16.234692 00:00 heroku [router]:at = info method = GET path =“/”host = krono-punch-clock.herokuapp.com request_id = 943d1695-e12e-4011-8564- b5561859a9da fwd =“100.13.50.153”dyno = web.1 connect = 0ms service = 69ms status = 500 bytes = 239 2016-07-28T21:43:16.387408 00:00 heroku [router]:at = info method = GET path = “/favicon.ico”host = krono-punch-clock.herokuapp.com request_id = 857e4d83-7b9d-4a83-aee9-d95976d41146 fwd =“100.13.50.153”dyno = web.1 connect = 0ms service = 56ms status = 404 bytes = 285 2016-07-28T21:43:16.433652 00:00 heroku [router]:at = info method = GET path =“/ favicon.ico”host = krono-punch-clock.herokuapp.com request_id = 5b298933-0536- 48c9-92ba-b4bae7b5b126 fwd =“100.13.50.153”dyno = web.1 connect = 0ms service = 4ms status = 404 bytes = 285

settings.py

import os

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


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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '29dh!3@ot91d*!60)0x%%9)ndtb)cn#4_uj72=^(@f#h!(fpfc'

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

ALLOWED_HOSTS = ['*']


# Application definition

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

    #user apps
    'kc_app',

    #third party apps
    'bootstrap3',
]

MIDDLEWARE_CLASSES = [
    'django.middleware.security.SecurityMiddleware',
    '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 = 'kronos_clock.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'kronos_clock.wsgi.application'


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

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


# Password validation
# https://docs.djangoproject.com/en/1.9/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/1.9/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/1.9/howto/static-files/
PROJECT_DIR = os.path.dirname(__file__)
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')

STATIC_URL = '/static/'

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

#settings for django-bootstrap3
BOOTSTRAP3 = {
    'include_jquery':True,
}

import dj_database_url
DATABASES['default'] = dj_database_url.config()

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

ALLOWED_HOSTS = ['*']

DEBUG = False

try:
    from .local_settings import *
except ImportError:
    pass

wsgi.py

import os
from django.core.wsgi import get_wsgi_application
from whitenoise.django import DjangoWhiteNoise


os.environ.setdefault("DJANGO_SETTINGS_MODULE", "kronos_clock.settings")

application = DjangoWhiteNoise(get_wsgi_application())

Procfile

我的Procfile是无扩展的 .

web: gunicorn kronos_clock.wsgi:application --log-file -

requirements.txt

dj-database-url==0.4.1
dj-static==0.0.6
Django==1.9.8
django-bootstrap3==7.0.1
django-toolbelt==0.0.1
gunicorn==19.6.0
psycopg2==2.6.2
static3==0.7.0
whitenoise==3.2

runtime.txt

python-3.5.1

我也一直在Windows 7上创建这个应用程序,如果有帮助的话 . 除此之外,我认为这是我现在想要添加的所有内容,但如果需要任何其他信息,请告诉我,我会为您发布 .

EDIT AND SOLUTION

好的,我明白了 . 有几个错误,但我会尽可能地解释它们以防万一其他人遇到这些相同的问题 .

首先,我在本地演示我的应用程序并想要解释管理站点 . 打开它时,它会出错,然后我记得我从未真正设置它 . 这是给出服务器错误(500)的原因,因为它在尝试访问管理站点时在本地发出了此错误 . 我很快创建了这个,然后继续下面 .

在我的views.py文件中,我有两个DATABASES的任务 . 因此,似乎存在一些冲突,导致Heroku出现一些错误 . 我的应用程序没有必要在当前用户的实例之外写入/保存数据到数据库 . 然而,这是我改变的:

我评论了这一行:

#DATABASES['default'] = dj_database_url.config()

并将其切换为:

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

if语句允许应用程序在未通过Heroku服务器运行时仍在本地运行 .

我也有重复的DEBUG分配,尽管两者都设置为False,为了清楚起见,我确实删除了一个,并避免将来的更改冲突 .

在DATABASES更改之后,我还将DEBUG更改回:

DEBUG = True

我推到了Heroku并打开了应用程序,幸运的是得到了一个有用的错误:

TemplateDoesNotExist at / kc_app \ base.html请求方法:GET请求URL:https://krono-punch-clock.herokuapp.com/ Django版本:1.9.8例外类型:TemplateDoesNotExist异常值:kc_app \ base.html异常位置:/app/.heroku/python/lib/python3.5/site-packages/django/template/engine.py在find_template中,第169行Python可执行文件:/app/.heroku / python / bin / python Python版本:3.5.1 Python路径:['/ app','/ app / .heroku / python / bin','/ app / .heroku / python / lib / python3.5 / site- packages / setuptools-23.1.0-py3.5.egg','/ app / .heroku / python / lib / python3.5 / site-package / patch8.1.2-py3.5.egg','/ app ','/ app / .heroku / python / lib / python35.zip','/ app / .heroku / python / lib / python3.5','/ app / .heroku / python / lib / python3.5 / plat -linux','/ app / .heroku / python / lib / python3.5 / lib-dynload','/ app / .heroku / python / lib / python3.5 / site-package']服务器时间:周六,30 2016年7月06:33:46 0000

在进一步阅读回溯后,我注意到这些线条:

模板加载器postmortem Django尝试按以下顺序加载这些模板:使用引擎django:django.template.loaders.app_directories.Loader:/app/.heroku/python/lib/python3.5/site-packages/django/contrib /admin/templates/kc_app\base.html
(来源不存在)django.template.loaders.app_directories.Loader:/app/.heroku/python/lib/python3.5/site-packages/django/contrib/auth/templates/kc_app\base.html(来源确实如此) django.template.loaders.app_directories.Loader:/app/kc_app/templates/kc_app\base.html /python3.5/site-packages/bootstrap3/templates/kc_app\base.html(源不存在)

请注意,以下行(从上面)是我的模板所在的位置:

django.template.loaders.app_directories.Loader:/app/kc_app/templates/kc_app\base.html(来源不存在)

仔细看,你会看到一个反斜杠而不是正斜杠 . 在这里,我知道这与Windows构建在Nix服务器上的Heroku服务器以及我在Windows中本地构建应用程序的区别 . 然后我更改了所有的views.py调用和模板,以在正斜杠上包含正斜杠 .

举些例子:

views.py example

return render(request, 'kc_app\\clock_out.html', 
                         {'kc_form':dict_form,'punch_out':final_out})

改为:

return render(request, 'kc_app/clock_out.html', 
                         {'kc_form':dict_form,'punch_out':final_out})

template example

{%extends“kc_app \ base.html”%}

改为:

{%extends“kc_app / base.html”%}

然后我将修复程序推回到Heroku,并且能够成功地使其在服务器上运行 .