Whenever debug=False in django, Heroku gives Server Error (500) and when debug=True no error

£可爱£侵袭症+ 提交于 2021-01-01 08:00:50

问题


When I set debug = False I get error 500 but when it is set to True I don't get any error.

I tried :-

  1. "heroku run python manage.py collectstatic"
  2. "commenting out whitenoise"
  3. "changing template and static and staticfiles locations"
  4. "COMPRESS_ENABLED = os.environ.get('COMPRESS_ENABLED', False)"
  5. "allowing everything in domain"
"""
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()

logs from 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"

This setting ran on my local server after commenting the ssl line

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

I was expecting it to force users to HTTPS site. I followed the heroku tutorial to deploy django app on their dev site.


回答1:


I Kind of solved myself do not use whitenoise, that's the solution for this problem. Remove whitenoise and good to go.



来源:https://stackoverflow.com/questions/55957618/whenever-debug-false-in-django-heroku-gives-server-error-500-and-when-debug-t

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!