Home > OS >  django server error (500) when DEBUG=False
django server error (500) when DEBUG=False

Time:09-17

This is base.py file

from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent.parent


SECRET_KEY = 'h@1l- Holla!!! this is something crazy, $2423$#@$#@E@e#R3\e['

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


INSTALLED_APPS = [
    # typical django code...

MIDDLEWARE = [
    # typical django code...
]

ROOT_URLCONF = 'project.urls'

TEMPLATES = [
    # typical django code...
]

WSGI_APPLICATION = 'project.wsgi.application'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}

AUTH_PASSWORD_VALIDATORS = [
    # typical django code...
]

STATIC_URL = '/static/'

# Static settings
STATICFILES_DIRS = [
    BASE_DIR / 'static',
]


# Media settings
MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR / 'media'


DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

This is production.py

from .base import *

import environ
import django_heroku

env = environ.Env(
    DEBUG=(bool, False),
)

SECRET_KEY = 'lol'

# right now for convenience I've hard-coded these
# despite having environ package
DEBUG = False
ALLOWED_HOSTS = ['*']

MIDDLEWARE.insert(1, 'whitenoise.middleware.WhiteNoiseMiddleware')

DATABASES['default']['CONN_MAX_AGE'] = 60

STATIC_ROOT = BASE_DIR / 'staticfiles'

STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'

# Activate Django-Heroku.
django_heroku.settings(locals())

I've renamed the original settings.py to base.py, and extended it to production.py. And also updated manage.py, wsgi.py and asgi.py.
Basically following this approach -> https://simpleisbetterthancomplex.com/tips/2017/07/03/django-tip-20-working-with-multiple-settings-modules.html

Everything looks just fine to me

screen shot of terminal - request error 500

This code is giving Server Error (500).
The problem is not in ALLOWED_HOSTS, that's for sure.

EDIT: few errors get fixed when I removed this line django_heroku.settings(locals()), though media files were still not loaded

Many people are facing the same problem and didn't get an appropriate answer yet!

Please help me with an answer with an explanation.

EDIT 2:

[08/Sep/2021 22:01:05] ERROR [django.request:224] Internal Server Error: /
Traceback (most recent call last):
  File "C:\Users\Vaibhav\.virtualenvs\project-nE60Z89-\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
    response = get_response(request)
  File "C:\Users\Vaibhav\.virtualenvs\project-nE60Z89-\lib\site-packages\django\core\handlers\base.py", line 204, in _get_response
    response = response.render()
  File "C:\Users\Vaibhav\.virtualenvs\project-nE60Z89-\lib\site-packages\django\template\response.py", line 105, in render
    self.content = self.rendered_content
  File "C:\Users\Vaibhav\.virtualenvs\project-nE60Z89-\lib\site-packages\django\template\response.py", line 83, in rendered_content
    return template.render(context, self._request)
  File "C:\Users\Vaibhav\.virtualenvs\project-nE60Z89-\lib\site-packages\django\template\backends\django.py", line 61, in render
    return self.template.render(context)
  File "C:\Users\Vaibhav\.virtualenvs\project-nE60Z89-\lib\site-packages\django\template\base.py", line 170, in render
    return self._render(context)
  File "C:\Users\Vaibhav\.virtualenvs\project-nE60Z89-\lib\site-packages\django\template\base.py", line 162, in _render
    return self.nodelist.render(context)
  File "C:\Users\Vaibhav\.virtualenvs\project-nE60Z89-\lib\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\Vaibhav\.virtualenvs\project-nE60Z89-\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "C:\Users\Vaibhav\.virtualenvs\project-nE60Z89-\lib\site-packages\django\template\loader_tags.py", line 150, in render
    return compiled_parent._render(context)
  File "C:\Users\Vaibhav\.virtualenvs\project-nE60Z89-\lib\site-packages\django\template\base.py", line 162, in _render
    return self.nodelist.render(context)
  File "C:\Users\Vaibhav\.virtualenvs\project-nE60Z89-\lib\site-packages\django\template\base.py", line 938, in render
    bit = node.render_annotated(context)
  File "C:\Users\Vaibhav\.virtualenvs\project-nE60Z89-\lib\site-packages\django\template\base.py", line 905, in render_annotated
    return self.render(context)
  File "C:\Users\Vaibhav\.virtualenvs\project-nE60Z89-\lib\site-packages\django\templatetags\static.py", line 106, in render
    url = self.url(context)
  File "C:\Users\Vaibhav\.virtualenvs\project-nE60Z89-\lib\site-packages\django\templatetags\static.py", line 103, in url
    return self.handle_simple(path)
  File "C:\Users\Vaibhav\.virtualenvs\project-nE60Z89-\lib\site-packages\django\templatetags\static.py", line 118, in handle_simple
    return staticfiles_storage.url(path)
  File "C:\Users\Vaibhav\.virtualenvs\project-nE60Z89-\lib\site-packages\django\contrib\staticfiles\storage.py", line 147, in url
    return self._url(self.stored_name, name, force)
  File "C:\Users\Vaibhav\.virtualenvs\project-nE60Z89-\lib\site-packages\django\contrib\staticfiles\storage.py", line 126, in _url
    hashed_name = hashed_name_func(*args)
  File "C:\Users\Vaibhav\.virtualenvs\project-nE60Z89-\lib\site-packages\django\contrib\staticfiles\storage.py", line 417, in stored_name
    raise ValueError("Missing staticfiles manifest entry for '%s'" % clean_name)
ValueError: Missing staticfiles manifest entry for 'home/css/base.css'
[08/Sep/2021 22:01:05] ERROR [django.server:161] "GET / HTTP/1.1" 500 145
[08/Sep/2021 22:01:05] WARNING [django.request:224] Not Found: /favicon.ico
[08/Sep/2021 22:01:05] WARNING [django.server:161] "GET /favicon.ico HTTP/1.1" 404 179

CodePudding user response:

you can log all errors to file with the below and it will log even when DEBUG=False


Update: Try this.

LOGGING = {
    'version': 1,
    'disable_existing_loggers': False,
    'filters': {
        'require_debug_false': {
            '()': 'django.utils.log.RequireDebugFalse'
        }
    },
    'formatters': {
        'verbose': {
            'format': '[contactor] %(levelname)s %(asctime)s %(message)s'
        },
    },
    'handlers': {
        # Send all messages to console
        'console': {
            'level': 'DEBUG',
            'class': 'logging.StreamHandler',
        },
        # Send info messages to syslog
        'syslog':{
            'level':'INFO',
            'class': 'logging.handlers.SysLogHandler',
            'facility': SysLogHandler.LOG_LOCAL2,
            'address': '/dev/log',
            'formatter': 'verbose',
        },
        # Warning messages are sent to admin emails
        'mail_admins': {
            'level': 'WARNING',
            'filters': ['require_debug_false'],
            'class': 'django.utils.log.AdminEmailHandler',
        },
        # critical errors are logged to sentry
        'sentry': {
            'level': 'ERROR',
            'filters': ['require_debug_false'],
            'class': 'raven.contrib.django.handlers.SentryHandler',
        },
    },
    'loggers': {
        # This is the "catch all" logger
        '': {
            'handlers': ['console', 'syslog', 'mail_admins', 'sentry'],
            'level': 'DEBUG',
            'propagate': False,
        },
    }
}
  • Related