调试=False时出现Django服务器错误(500)

2024-09-30 13:19:56 发布

您现在位置:Python中文网/ 问答频道 /正文

这是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'

这是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.pywsgi.pyasgi.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)
问题是ALLOWED_HOSTS中的不是,这是肯定的

编辑:当我删除这一行时,几乎没有错误得到修复,尽管媒体文件仍然没有加载

很多人都面临着同样的问题,但还没有得到一个合适的答案

请帮我回答并解释一下

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


Tags: djangoinpyprojectlibpackageslinesite
1条回答
网友
1楼 · 发布于 2024-09-30 13:19:56

您可以将所有错误记录到下面的文件中,即使在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,
        },
    }
}

相关问题 更多 >

    热门问题