【问题标题】:Django app deploy on Heroku with DEBUG=False Server Error 500Django 应用程序部署在 Heroku 上,DEBUG=False 服务器错误 500
【发布时间】:2017-10-20 20:09:00
【问题描述】:

我有一个应用程序部署到 Heroku。它在本地和 Heroku 上运行良好,带有“DEBUG = True”。但是,当我在 Heroku 上使用“DEBUG=False”运行它时,我得到一个服务器错误 500,如下所示:

2017-05-20T22:00:26.864342+00:00 heroku[router]: at=info method=GET path="/plans/planslist/" host=app.mysite.com request_id=b9a4de90-4a81-42ff-b936-59aa3d17e02e fwd="50.181.204.79" dyno=web.1 connect=0ms service=443ms status=500 bytes=253 protocol=http
2017-05-20T22:00:27.304092+00:00 heroku[router]: at=info method=GET path="/favicon.ico" host=app.mysite.com request_id=b24922e4-974a-45db-8ce0-def179602203 fwd="50.181.204.79" dyno=web.1 connect=1ms service=57ms status=500 bytes=239 protocol=http

我一定是设置错误,但无法弄清楚。任何帮助将不胜感激。我已经破解了半天,但没有得到任何地方。

我的设置.py

import os
from django.conf import settings
from .base import *

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

DEBUG = False
TEMPLATE_DEBUG = False

import dj_database_url
DATABASES = { 'default': dj_database_url.config(conn_max_age=500) }

ALLOWED_HOSTS = ['app.mysite.com']

INSTALLED_APPS = [
    'django.contrib.auth',
    'django.contrib.sites',
    'django.contrib.humanize',
    'bootstrap3',
    'company_account',
    'company_data',
    'plans',
    'django.contrib.admin',
    'registration',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    ]

# Usual Middleware here

WSGI_APPLICATION = 'mywebapp.wsgi.application'

STATIC_URL = '/collectstatic/'
STATIC_ROOT = 'staticfiles'

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
    )

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

【问题讨论】:

  • 尝试像这样设置允许的主机:ALLOWED_HOSTS = ['.mysite.com']
  • @KostasLivieratos 试过了,但没有帮助。
  • 你们有日志服务吗?这将有助于获取错误回溯

标签: django heroku deployment


【解决方案1】:

尝试在 Heroku 上部署 DEBUG=False 的 Django 应用时出现服务器错误 (500),可以通过安装解决

pip 安装白噪声

编辑您的 settings.py 文件并将 WhiteNoise 添加到 MIDDLEWARE 列表中,高于除 Django 的 SecurityMiddleware 之外的所有其他中间件:

MIDDLEWARE = [
  # 'django.middleware.security.SecurityMiddleware',
  'whitenoise.middleware.WhiteNoiseMiddleware',
  # ...
]

如果您安装了django-heroku,请尝试将其注释掉,并将django_heroku.settings(locals()) 注释掉。

你可以把你的DEBUG = False 重新部署到 Heroku。 这种方法实际上对我有用。你可以试试看。

您也可以阅读whitenoise 文档:http://whitenoise.evans.io/en/stable/

【讨论】:

    猜你喜欢
    • 2021-10-19
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 2018-12-30
    • 2021-11-05
    • 2019-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多