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

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

问题描述

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


  1. Debug = True&& DB = local =>运行良好

  2. Debug = True&& DB = production =>运行良好

  3. Debug = False&& DB = local =>运行良好

  4. Debug = False&& DB = Production => Server 500错误

第四个同时是:最重要的,最难调试的,也是唯一一个失败了。



我们的django设置是以这种结构设置的:

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

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

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

  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 使用的文件:

  from project.settings.base import * 

ALLOWED_HOSTS = ['*']

DATABASES = {
'default':{
'ENGINE':'django.db.backends.postgresql_psycopg2',
'NAME':***,
'USER':***,
'密码':***,
'主机':'***。amazonaws.com',
'PORT':'5432',
}
}

我们也用Debug = True和DB = production以及Debug = False和DB = local运行它,两者都可以工作。

数据库设置直接从Heroku只要将Debug设置为True,配置和与数据库的连接就会很好,所以我们确信它不是数据库模式或连接问题。当Debug为True时,我们无法弄清楚生产数据库是如何工作的,并且在DB使用本地数据库设置为False的情况下运行,但由于某种原因,两者结合失败。我们还将代码部署到了Heroku,并确认它在Debug设置为True时运行,但在Debug设置为False时出现相同的Server 500错误。



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

  import os 
BASE_DIR = os.path.dirname(os.path.dirname(__ file__))

#快速启动开发设置 - 不适合制作
#请参阅https:// docs.djangoproject.com/en/1.6/howto/deployment/checklist/

#安全警告:保持秘密密钥在生产中使用!
SECRET_KEY = ***


#应用程序定义

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'

#数据库
#https://docs.djangoproject.com/en/1.6/ref/settings/#databases

SECURE_PROXY_SSL_HEADER =('HTTP_X_FORWARDED_PROTO','https')

#国际化
#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

#静态文件(CSS,JavaScript,图片)
#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变量进行错误配置,最终出现类似的症状,但这似乎不是我们的问题。有没有人对导致这个问题的原因有所了解?



根据要求,这里是production.py,不过应该注意的是,这个设置文件从来没有用在这个实验中。

  from project.settings.base import * 

import dj_database_url

DEBUG = False
TEMPLATE_DEBUG = False

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

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

STATICFILES_STORAGE ='whitenoise.django.GzipManifestStaticFilesStorage'


解决方案

我有同样的问题。但后来我在settings.py中删除了
这一行。

pre $ ST $ F $ b

现在,当DEBUG = False时,我没有500错误。但可能,我想gzip功能不再工作。

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

  1. Debug=True && DB=local => Runs fine
  2. Debug=True && DB=production => Runs fine
  3. Debug=False && DB=local => Runs fine
  4. Debug=False && DB=Production => Server 500 error

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

Our django settings are setup with this structure:

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

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

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',
    }
}

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',
    }
}

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

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.

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'), )

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?

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'

解决方案

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

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

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

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

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