Python 当Debug=False时,Django返回500内部服务器错误,而不是index.html

Python 当Debug=False时,Django返回500内部服务器错误,而不是index.html,python,django,heroku,django-templates,http-status-code-500,Python,Django,Heroku,Django Templates,Http Status Code 500,我的django web应用程序中有一个问题,django在尝试获取index.html文件时返回500错误。这仅在Debug=False时发生,并且仅在使用此模板时发生。所有其他模板正常渲染,没有错误 我已经尝试了白噪声设置,favicon.ico错误,检查了所有路线,一切似乎都很好,我真的找不到错误。奇怪的是,它只发生在index.html中 如果有人能帮助我,我会非常感激,提前谢谢 设置.py DEBUG = False ALLOWED_HOSTS = ['vilytic.herokuap

我的django web应用程序中有一个问题,django在尝试获取index.html文件时返回500错误。这仅在Debug=False时发生,并且仅在使用此模板时发生。所有其他模板正常渲染,没有错误

我已经尝试了白噪声设置,favicon.ico错误,检查了所有路线,一切似乎都很好,我真的找不到错误。奇怪的是,它只发生在index.html中

如果有人能帮助我,我会非常感激,提前谢谢

设置.py

DEBUG = False
ALLOWED_HOSTS = ['vilytic.herokuapp.com', '127.0.0.1']

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'accounts',
    'comparer',
]

REST_FRAMEWORK = {
    'DEFAULT_THROTTLE_RATES': {
        'video_search': '8/day',
        'video_id': '8/day'
    }
}

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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',
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        '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',
            ],
        },
    },
]

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
from django.contrib import admin
from django.urls import path, include
from . import views
from django.views.generic.base import RedirectView
from django.conf import settings
from django.contrib.staticfiles.storage import staticfiles_storage

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index, name="index"),
    path('accounts/', include('accounts.urls'), name="accounts"),
    path('comparer/', include('comparer.urls'), name="comparer"),
    path('contact/', views.contact, name="contact"),
    path(
        "favicon.ico",
        RedirectView.as_view(url=staticfiles_storage.url("favicon.ico")),
    ),
]

from django.shortcuts import render
from django.core.mail import EmailMessage
from django.template.loader import render_to_string
from django.conf import settings


def index(request):
    return render(request, 'index.html')
终端中出现的错误

"GET / HTTP/1.1" 500 145
url.py

DEBUG = False
ALLOWED_HOSTS = ['vilytic.herokuapp.com', '127.0.0.1']

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'accounts',
    'comparer',
]

REST_FRAMEWORK = {
    'DEFAULT_THROTTLE_RATES': {
        'video_search': '8/day',
        'video_id': '8/day'
    }
}

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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',
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        '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',
            ],
        },
    },
]

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
from django.contrib import admin
from django.urls import path, include
from . import views
from django.views.generic.base import RedirectView
from django.conf import settings
from django.contrib.staticfiles.storage import staticfiles_storage

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index, name="index"),
    path('accounts/', include('accounts.urls'), name="accounts"),
    path('comparer/', include('comparer.urls'), name="comparer"),
    path('contact/', views.contact, name="contact"),
    path(
        "favicon.ico",
        RedirectView.as_view(url=staticfiles_storage.url("favicon.ico")),
    ),
]

from django.shortcuts import render
from django.core.mail import EmailMessage
from django.template.loader import render_to_string
from django.conf import settings


def index(request):
    return render(request, 'index.html')
视图.py

DEBUG = False
ALLOWED_HOSTS = ['vilytic.herokuapp.com', '127.0.0.1']

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'rest_framework',
    'accounts',
    'comparer',
]

REST_FRAMEWORK = {
    'DEFAULT_THROTTLE_RATES': {
        'video_search': '8/day',
        'video_id': '8/day'
    }
}

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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',
]

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        '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',
            ],
        },
    },
]

STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
from django.contrib import admin
from django.urls import path, include
from . import views
from django.views.generic.base import RedirectView
from django.conf import settings
from django.contrib.staticfiles.storage import staticfiles_storage

urlpatterns = [
    path('admin/', admin.site.urls),
    path('', views.index, name="index"),
    path('accounts/', include('accounts.urls'), name="accounts"),
    path('comparer/', include('comparer.urls'), name="comparer"),
    path('contact/', views.contact, name="contact"),
    path(
        "favicon.ico",
        RedirectView.as_view(url=staticfiles_storage.url("favicon.ico")),
    ),
]

from django.shortcuts import render
from django.core.mail import EmailMessage
from django.template.loader import render_to_string
from django.conf import settings


def index(request):
    return render(request, 'index.html')

这意味着您有一个错误,因为
DEBUG=FALSE
它不会显示错误。您可以在
控制台
中看到错误,或者通过启用
调试

我以前在使用Whitenoise时遇到过这种情况,很难找到原因。当然,假设在
DEBUG=True
时没有错误,请尝试设置

WHITENOISE_AUTOREFRESH = True

在您的
settings.py

中,原来是png文件导致了问题。我不知道为什么png文件有时会用大写字母保存,比如png。显然,这是白噪音的问题。我花了很长时间才弄明白,希望能有所帮助。

我的在获取索引文件时也出现了类似的错误

发生错误的原因是我的URL配置如下:

path('admin/', admin.site.urls)
并试图摆脱/


您可以从路径中删除/或获取。

不一定。我在使用Whitenoise时遇到过这个问题–找到问题可能比这更棘手。每次出现这个错误时,我都会启用
DEBUG
检查错误并修复它。我的意思是,在没有错误的情况下,这个问题可能会发生–这在我使用Whitenoise时发生过。哦,很高兴知道