当 Debug = False 时,Django 返回 500 Internal Server Error 而不是 index.html

Django returning 500 Internal Server Error instead of index.html when Debug = False

我的 Django Web 应用程序出现问题,其中 Django returns 在尝试获取 index.html 文件时出现 500 错误。这只会在 Debug = False 时发生,并且只会发生在这个模板上。所有其他模板正常呈现,没有错误。

我已经尝试过whitenoise设置,favicon.ico错误,检查了所有路由似乎一切正常,我真的找不到错误。奇怪的是它只发生在 index.html.

如果有人能提供帮助,我将不胜感激,在此先感谢。

settings.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'

终端出现错误

"GET / HTTP/1.1" 500 145

urls.py

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")),
    ),
]

views.py

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它没有显示错误。您可以在 console 或启用 DEBUG

中查看错误

我以前用 Whitenoise 遇到过这个问题,很难找到原因。当然假设你在 DEBUG = True 时没有错误,尝试设置

WHITENOISE_AUTOREFRESH = True

在你的 settings.py.

原来是 png 文件导致了问题。我不知道为什么 png 文件有时会用大写字母保存,比如 PNG。显然,这导致了白噪声问题。弄了好久,希望对你有帮助

我在尝试获取索引文件时遇到了类似的错误。

发生错误是因为我的网址是这样配置的:

path('admin/', admin.site.urls)

并尝试在没有 /.

的情况下获得 http://127.0.0.1:8000/admin

您可以从路径中删除 / 或获取 http://127.0.0.1:8000/admin/.

对我来说,static 模板标签有问题,但 white-nose 确实为我提供了有关 issue/error 的任何有用信息。

静态文件地址错误:

<link rel="icon" href="{% static 'images/favicon.png' %}">

应该是

<link rel="icon" href="{% static 'blog/images/favicon.png' %}">

更正地址后,它解决了我的问题。