为什么只有当 Debug=False AND db 设置为 Heroku 上的生产数据库时,django 才会在服务器 500 上失败? [英] Why would django fail with server 500 only when Debug=False AND db is set to production database on Heroku?

查看:20
本文介绍了为什么只有当 Debug=False AND db 设置为 Heroku 上的生产数据库时,django 才会在服务器 500 上失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们运行 $ python manage.py runserver --settings=project.settings.local 时,有 4 种不同的可能组合:

When we run $ python manage.py runserver --settings=project.settings.local there are 4 different possible combinations:

  1. 调试=真 &&DB=local => 运行良好
  2. 调试=真 &&DB=production => 运行良好
  3. Debug=False &&DB=local => 运行良好
  4. Debug=False &&DB=Production => 服务器 500 错误

第四个同时:最重要,最难调试,唯一失败.

The fourth one is simultaneously: the most important, the hardest to debug, and the only one that fails.

我们的 django 设置是用这个结构设置的:

Our django settings are setup with this structure:

settings
├── base.py
├── __init__.py
├── local.py
└── production.py

对于这个测试,我们只使用了 local.py 并修改了每次运行的内容.

For this test we only used local.py and modified the contents for each run.

对于 Debug=True 和 DB=local,这是 local.py 文件:

For Debug=True and DB=local this is the local.py file:

from project.settings.base import *

DEBUG = True
TEMPLATE_DEBUG = True

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': ***,
        'USER': ***,
        'PASSWORD': ***,
        'HOST': 'localhost',
        'PORT': '5432',
    }
}

对于 Debug=False 和 DB=production,这是使用的 local.py 文件:

For Debug=False and DB=production this is the local.py file used:

from project.settings.base import *

ALLOWED_HOSTS = ['*']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': ***,
        'USER': ***,
        'PASSWORD': ***,
        'HOST': '***.amazonaws.com',
        'PORT': '5432',
    }
}

我们还使用 Debug=True 和 DB=production 以及 Debug=False 和 DB=local 运行它,两者都有效.

We also ran it with Debug=True and DB=production as well as Debug=False and DB=local, both of which worked.

数据库设置是直接从 Heroku 配置复制的,只要 Debug 设置为 True,与数据库的连接就可以正常工作,因此我们非常确定这不是数据库架构或连接问题.我们只是无法弄清楚当 Debug 为 True 时生产 DB 是如何工作的,并且它在 DB 设置为 False 与本地 DB 的情况下运行,但由于某种原因,当两者结合时它会失败.我们还将代码部署到 Heroku 并确认它在 Debug 设置为 True 的情况下运行,但在 Debug 设置为 False 时失败并出现相同的 Server 500 错误.

The DB settings were copied directly from the Heroku configuration and the connection to the database works great as long as Debug is set to True, so we're pretty sure it's not a DB schema or connection problem. We just can't figure out how it's possible that the production DB works when Debug is True, and it runs with DB set to False with the local DB, but for some reason when the two are combined it fails. We've also deployed the code to Heroku and confirmed that it runs with Debug set to True but fails with the same Server 500 error when Debug is set to False.

作为参考,这是我们base.py的内容:

For reference, this is the contents of our base.py:

import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

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

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ***


# Application definition

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

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

ROOT_URLCONF = 'project.urls'

WSGI_APPLICATION = 'project.wsgi.application'

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

SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

# Internationalization
# https://docs.djangoproject.com/en/1.6/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.6/howto/static-files/

STATIC_ROOT = 'staticfiles'
STATIC_URL = '/static/'
STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static'), )

我们通过谷歌搜索发现很多人错误地配置了 ALLOWED_HOSTS 变量并以类似的症状结束,但这似乎不是我们的问题.有没有人对可能导致此问题的原因有任何见解?

Our googling has come up with a lot of people misconfiguring the ALLOWED_HOSTS variable and ending up with similar symptoms but that doesn't seem to be our problem. Does anybody have any insight as to what could be causing this problem?

根据要求,这里是production.py,但需要注意的是,这个设置文件从未在本实验中使用过.

As requested, here is production.py, although it should be noted that this settings file was never used in this experiment.

from project.settings.base import *

import dj_database_url

DEBUG = False
TEMPLATE_DEBUG = False

ALLOWED_HOSTS = ['*', '.***.com', '.herokuapp.com', 'localhost', '127.0.0.1']

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': ***,
        'USER': ***,
        'PASSWORD': ***,
        'HOST': '***.amazonaws.com',
        'PORT': '5432',
    }
}

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

推荐答案

我遇到了同样的问题.但后来我删除了settings.py 中的这一行

I have had the same issue. But then I removed this row in settings.py

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

现在当 DEBUG=False 时我没有 500 错误.但可能,我猜 gzip 功能不再起作用了.

Now I dont have 500 error when DEBUG=False. But probably, I guess gzip functionality doesnt work anymore.

这篇关于为什么只有当 Debug=False AND db 设置为 Heroku 上的生产数据库时,django 才会在服务器 500 上失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆