每当 django 中的 debug=False 时,Heroku 都会给出服务器错误(500),而当 debug=True 时没有错误

Posted

技术标签:

【中文标题】每当 django 中的 debug=False 时,Heroku 都会给出服务器错误(500),而当 debug=True 时没有错误【英文标题】:Whenever debug=False in django, Heroku gives Server Error (500) and when debug=True no error 【发布时间】:2019-09-21 06:44:20 【问题描述】:

当我设置 debug = False 时,我得到错误 500,但当它设置为 True 时,我没有得到任何错误。

我试过了:-

    “heroku 运行 python manage.py collectstatic” “注释掉白噪声” “更改模板以及静态和静态文件位置” “COMPRESS_ENABLED = os.environ.get('COMPRESS_ENABLED', False)” “允许域中的所有内容”
"""
Django settings for sites project.

Generated by 'django-admin startproject' using Django 2.2.

For more information on this file, see
https://docs.djangoproject.com/en/2.2/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/2.2/ref/settings/
"""

import os
import whitenoise
import dj_database_url
import django_heroku

# 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/2.2/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
#''
SECRET_KEY = os.environ['DJANGO_SECRET_KEY']

# 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',
    'django.contrib.sites',
    'django.contrib.sitemaps',
    'blog',
    'django_wysiwyg',
    'ckeditor',
    'widget_tweaks',
]

MIDDLEWARE = [
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.middleware.security.SecurityMiddleware',
    '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',
    'django.middleware.security.SecurityMiddleware',
]

ROOT_URLCONF = 'sites.urls'
SITE_ID = 1
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 = 'sites.wsgi.application'


# Database
# https://docs.djangoproject.com/en/2.2/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/2.2/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/2.2/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'Asia/Kolkata'

USE_I18N = True

USE_L10N = True

USE_TZ = True

# Static files (CSS, javascript, Images)
# https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

STATIC_URL = '/static/'

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, 'static'),
)
#  Add configuration for static files storage using whitenoise
#STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

#Prod Database


DJANGO_WYSIWYG_FLAVOR = "ckeditor"
django_heroku.settings(locals())

#Production 
CSRF_COOKIE_SECURE = True
SESSION_COOKIE_SECURE = True
SECURE_SSL_REDIRECT = True
SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

requirements.txt

dj-database-url==0.5.0
Django==2.2
django-ckeditor==5.7.0
django-heroku==0.3.1
django-js-asset==1.2.2
django-widget-tweaks==1.4.3
django-wysiwyg==0.8.0
gunicorn==19.9.0
psycopg2==2.8.2
psycopg2-binary==2.8.2
pytz==2019.1
sqlparse==0.3.0
whitenoise==4.1.2

wsgi.py

"""
WSGI config for sites project.

It exposes the WSGI callable as a module-level variable named ``application``.

For more information on this file, see
https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/
"""

import os

from django.core.wsgi import get_wsgi_application


os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'sites.settings')

application = get_wsgi_application()

heroku 的日志

2019-05-02T17:28:58.987520+00:00 app[web.1]: [2019-05-02 17:28:58 +0000] [4] [INFO] Using worker: sync
2019-05-02T17:28:58.991090+00:00 app[web.1]: [2019-05-02 17:28:58 +0000] [10] [INFO] Booting worker with pid: 10
2019-05-02T17:28:59.099019+00:00 app[web.1]: [2019-05-02 17:28:59 +0000] [11] [INFO] Booting worker with pid: 11
2019-05-02T17:29:00.617376+00:00 heroku[web.1]: State changed from starting to up
2019-05-02T17:29:02.000000+00:00 app[api]: Build succeeded
2019-05-02T17:29:24.459932+00:00 heroku[router]: at=info method=GET path="/" host=www.sites.com request_id=e01ecca-9401-4672-95b8-fa0e88a8a8c8 fwd="47.30.149.115" dyno=web.1 connect=0ms service=929ms status=500 bytes=234 protocol=https
2019-05-02T17:29:24.458064+00:00 app[web.1]: 10.30.222.9 - - [02/May/2019:22:59:24 +0530] "GET / HTTP/1.1" 500 27 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/73.0.3683.103 Safari/537.36"
2019-05-02T17:29:25.821391+00:00 heroku[router]: at=info method=GET path="/" host=www.sites.com request_id=f748924-a57f-4a3f-858b-6be376c9efcc fwd="47.30.149.115" dyno=web.1 connect=1ms service=1083ms status=500 bytes=234 protocol=https
2019-05-02T17:29:25.818762+00:00 app[web.1]: 10.52.4.51 - - [02/May/2019:22:59:25 +0530] "GET / HTTP/1.1" 500 27 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.103 Safari/537.36"

注释 ssl 行后,此设置在我的本地服务器上运行

MIDDLEWARE = [
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.middleware.security.SecurityMiddleware',
    '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',
    #'django.middleware.security.SecurityMiddleware',
]

# CSRF_COOKIE_SECURE = True
# SESSION_COOKIE_SECURE = True
# SECURE_SSL_REDIRECT = True
# SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')

我期待它强制用户访问 HTTPS 站点。 我按照 heroku 教程在他们的开发网站上部署 django 应用程序。

【问题讨论】:

服务器日志说什么?你试过heroku logs吗? 是的。没有什么有用的只显示错误 500。我认为问题与 collectstatics 有某种关系 【参考方案1】:

我有点解决了自己不使用白噪声,这就是这个问题的解决方案。 去除白噪声,一切顺利。

【讨论】:

以上是关于每当 django 中的 debug=False 时,Heroku 都会给出服务器错误(500),而当 debug=True 时没有错误的主要内容,如果未能解决你的问题,请参考以下文章

Django3.0:来自数据库的图像在 debug=False 后未显示

解决Django设置debug=False静态文件获取不到的问题

Django 无法识别 DEBUG = False

使用 DEBUG=False,我如何将 django 异常记录到日志文件中

当 debug=False 时可能导致 Django 错误,而当 debug=True 时不存在

Django 中 DEBUG = True 和 False 之间的功能区别是啥?