简体   繁体   English

为什么只有当 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?

When we run $ python manage.py runserver --settings=project.settings.local there are 4 different possible combinations:当我们运行$ python manage.py runserver --settings=project.settings.local ,有 4 种不同的可能组合:

  1. Debug=True && DB=local => Runs fine Debug=True && DB=local => 运行良好
  2. Debug=True && DB=production => Runs fine Debug=True && DB=production => 运行良好
  3. Debug=False && DB=local => Runs fine Debug=False && DB=local => 运行良好
  4. Debug=False && DB=Production => Server 500 error Debug=False && DB=Production => 服务器 500 错误

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:我们的 django 设置是使用以下结构设置的:

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

For this test we only used local.py and modified the contents for each run.对于这个测试,我们只使用了local.py并修改了每次运行的内容。

For Debug=True and DB=local this is the local.py file:对于 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',
    }
}

For Debug=False and DB=production this is the local.py file used:对于 Debug=False 和 DB=production,这是使用的local.py文件:

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.我们还使用 Debug=True 和 DB=production 以及 Debug=False 和 DB=local 运行它,这两个都有效。

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

For reference, this is the contents of our base.py :作为参考,这是我们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.我们通过谷歌搜索发现很多人错误地配置了 ALLOWED_HOSTS 变量并以类似的症状结束,但这似乎不是我们的问题。 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.根据要求,这里是 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']

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但后来我在 settings.py 中删除了这一行

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

Now I dont have 500 error when DEBUG=False.现在当 DEBUG=False 时我没有 500 错误。 But probably, I guess gzip functionality doesnt work anymore.但可能,我猜 gzip 功能不再起作用了。

What you should be doing here is this:你应该在这里做的是:

  • Open up your Heroku logs by running $ heroku logs --tail in one terminal window.通过在一个终端窗口中运行$ heroku logs --tail打开您的 Heroku 日志。
  • In another terminal window run $ heroku ps:restart to restart your dynos.在另一个终端窗口中运行$ heroku ps:restart以重新启动您的 dynos。

Watch the logs, and see the actual traceback.观察日志,并查看实际的回溯。 This will tell you exactly what's going on.这会告诉你到底发生了什么。 Based on your configuration it could be a number of issues.根据您的配置,这可能是许多问题。

The solution for me was to run python manage.py collectstatic .我的解决方案是运行python manage.py collectstatic

If you run $ heroku logs --tail inside terminal before doing this, the log will tell you what static files cannot be found (404).如果您在执行此操作之前在终端内运行$ heroku logs --tail ,日志会告诉您找不到哪些静态文件(404)。

Configuring the server e-mail and seeing the stack trace as mentioned by Two-Bit Alchemist was the key.配置服务器电子邮件并查看两位炼金术士提到的堆栈跟踪是关键。 We added these lines to our settings:我们将这些行添加到我们的设置中:

EMAIL_HOST = 'smtp.gmail.com'
EMAIL_HOST_USER = '***'
EMAIL_HOST_PASSWORD = '***'
EMAIL_PORT = 587
EMAIL_USE_TLS = True
SERVER_EMAIL = EMAIL_HOST_USER

And received an e-mail with this error in the stack trace:并在堆栈跟踪中收到一封包含此错误的电子邮件:

ValueError: The file 'stylesheets/application.css' could not be found with <whitenoise.django.GzipManifestStaticFilesStorage object at 0x7fdcebb94550>.

We'd had problems with static files before but though we'd fixed them.我们之前遇到过静态文件的问题,但我们已经修复了它们。 The css file it's complaining about not being able to find doesn't exist anymore and isn't referenced anywhere so I'm not sure why this error is even coming up, but we fixed it by adding an empty application.css file.它抱怨找不到的 css 文件不再存在,也没有在任何地方引用,所以我不确定为什么会出现这个错误,但我们通过添加一个空的application.css文件来修复它。

I hope this helps someone someday.我希望有一天这对某人有所帮助。 I had this similar problems and it took a while before i fixed it.我遇到了类似的问题,花了一段时间才修复它。 Check your <a></a> tags.检查您的<a></a>标签。 If an href attributes points to a template/link that don't exist.如果href属性指向不存在的模板/链接。 You might encounter such .您可能会遇到这样的.

something here helped me:这里的一些东西帮助了我:

I had a static css file link (and honestly, it had just a few lines), once I removed the link, and inserted into a tag in my main.html, it worked just fine: with DEBUG=False我有一个静态 css 文件链接(老实说,它只有几行),一旦我删除了链接,并插入到我的 main.html 中的标签中,它就可以正常工作:使用 DEBUG=False

:D :D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当 debug 设置为 False 时,在 Heroku 上部署 Django 时出现服务器错误 500 - Server error 500 on Django when deployed on Heroku when debug is set to False 使用 Heroku 和 Django 时 Debug=False 时出现 500 错误 - 500 error when Debug=False with Heroku and Django 当生产中的debug = False时500服务器错误 - 500 server error when debug=False in production 每当django中debug = False时,Heroku会显示Server Error(500),而debug = True no error时 - Whenever debug=False in django, Heroku gives Server Error (500) and when debug=True no error 当 DEBUG=False 时 django 服务器错误 (500) - django server error (500) when DEBUG=False 尝试在 heroku 上使用 DEBUG=False 部署 django 应用程序时出现服务器错误 (500) - Server error (500) when trying to deploy django app with DEBUG=False on heroku 仅当调试设置为 False 时,生产中出现错误 500 - Error 500 in production just when debug is set to False 当 django 中的 Debug = False 时,为什么我在我的网站上看到服务器错误 500 - Why i am see server error 500 in my website when Debug = False in django 当DEBUG = FALSE时Django 500错误,但仅在某些页面上 - Django 500 Error when DEBUG=FALSE, but only on some pages 当Debug = False时,在Heroku中出现500错误 - 500 error in Heroku, when Debug=False
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM