DEBUG=False 时 Django 服务器错误 (500)
django server error (500) when DEBUG=False
这是base.py
文件
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent.parent
SECRET_KEY = 'h@1l- Holla!!! this is something crazy, 23$#@$#@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'
这是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())
我已将原来的 settings.py
重命名为 base.py
,并将其扩展为 production.py
。并且还更新了 manage.py
、wsgi.py
和 asgi.py
.
基本上遵循这种方法 -> https://simpleisbetterthancomplex.com/tips/2017/07/03/django-tip-20-working-with-multiple-settings-modules.html
我觉得一切都很好
screen shot of terminal - request error 500
此代码给出 服务器错误 (500)。
问题是不是 in ALLOWED_HOSTS
,这是肯定的
编辑: 当我删除此行 django_heroku.settings(locals())
时,很少有错误得到修复,尽管媒体文件仍未加载
很多人都面临同样的问题,还没有得到合适的答案!
请帮我解释一下答案。
编辑 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
您可以使用以下内容将所有错误记录到文件中,即使在 DEBUG=False
时也会记录
更新:试试这个。
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,
},
}
}
这是base.py
文件
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent.parent
SECRET_KEY = 'h@1l- Holla!!! this is something crazy, 23$#@$#@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'
这是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())
我已将原来的 settings.py
重命名为 base.py
,并将其扩展为 production.py
。并且还更新了 manage.py
、wsgi.py
和 asgi.py
.
基本上遵循这种方法 -> https://simpleisbetterthancomplex.com/tips/2017/07/03/django-tip-20-working-with-multiple-settings-modules.html
我觉得一切都很好
screen shot of terminal - request error 500
此代码给出 服务器错误 (500)。
问题是不是 in ALLOWED_HOSTS
,这是肯定的
编辑: 当我删除此行 django_heroku.settings(locals())
时,很少有错误得到修复,尽管媒体文件仍未加载
很多人都面临同样的问题,还没有得到合适的答案!
请帮我解释一下答案。
编辑 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
您可以使用以下内容将所有错误记录到文件中,即使在 DEBUG=False
更新:试试这个。
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,
},
}
}