简体   繁体   English

当 DEBUG=False 时 django 服务器错误 (500)

[英]django server error (500) when DEBUG=False

This is base.py file这是base.py文件

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这是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 .我已将原始settings.py重命名为base.py ,并将其扩展为production.py And also updated manage.py , wsgi.py and asgi.py .并且还更新manage.pywsgi.pyasgi.py
Basically following this approach -> https://simpleisbetterthancomplex.com/tips/2017/07/03/django-tip-20-working-with-multiple-settings-modules.html基本上遵循这种方法-> https://simpleisbetterthancomplex.com/tips/2017/07/03/django-tip-20-working-with-multiple-settings-modules.html

Everything looks just fine to me在我看来一切都很好

在此处输入图像描述

This code is giving Server Error (500) .此代码给出Server Error (500)
The problem is not in ALLOWED_HOSTS , that's for sure.问题不在ALLOWED_HOSTS中,这是肯定的。

EDIT: few errors get fixed when I removed this line django_heroku.settings(locals()) , though media files were still not loaded编辑:当我删除这一行django_heroku.settings(locals())时,很少有错误得到修复,尽管媒体文件仍然没有加载

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:编辑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

you can log all errors to file with the below and it will log even when DEBUG=False您可以使用以下内容将所有错误记录到文件中,即使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,
        },
    }
}

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

相关问题 使用 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 当 DEBUG=False 时服务器错误 500 - Server Error 500 when DEBUG=False 每当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 时,在 Heroku 上部署 Django 时出现服务器错误 500 - Server error 500 on Django when deployed on Heroku when debug is set to False 将Debug更改为False后,Django Server Error 500 - Django Server Error 500 after changing Debug to False 当 DEBUG=False 时,我的 Django 管理面板给出服务器错误(500) - My Django admin panel gives 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 Django 应用程序在 Debug = False 时在本地主机上获取服务器错误 500 - Django app getting server error 500 on local host when Debug = False 当 django 中的 Debug = False 时,为什么我在我的网站上看到服务器错误 500 - Why i am see server error 500 in my website when Debug = False in django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM