简体   繁体   English

每当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

When I set debug = False I get error 500 but when it is set to True I don't get any error. 当我将debug设置为False时,我会收到错误500,但当它设置为True时,我不会收到任何错误。

I tried :- 我试过了 :-

  1. "heroku run python manage.py collectstatic" “ heroku运行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)" “ 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 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.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 来自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 注释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')

I was expecting it to force users to HTTPS site. 我期望它能迫使用户访问HTTPS站点。 I followed the heroku tutorial to deploy django app on their dev site. 我按照heroku教程在他们的开发站点上部署了django应用。

I Kind of solved myself do not use whitenoise, that's the solution for this problem. 我自己解决了一点,不用白噪声,这就是解决此问题的方法。 Remove whitenoise and good to go. 消除白噪声,一切顺利。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM