【问题标题】:Django returning 500 Internal Server Error instead of index.html when Debug = False当 Debug = False 时,Django 返回 500 Internal Server Error 而不是 index.html
【发布时间】:2021-04-24 23:36:50
【问题描述】:

我的 django Web 应用程序出现问题,django 在尝试获取 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')

【问题讨论】:

    标签: python django heroku django-templates http-status-code-500


    【解决方案1】:

    这意味着你有一个错误,因为DEBUG = FALSE 它没有显示错误。您可以在console 或启用DEBUG 中查看错误

    【讨论】:

    • 不一定。我在 Whitenoise 上遇到过这个问题 - 找到问题可能比这更棘手。
    • 每次我遇到这个错误时,我都会启用DEBUG 检查错误是什么并修复它。
    • 我的意思是,当没有错误时可能会发生此问题——我在使用 whitenoise 时发生了这种情况。
    【解决方案2】:

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

    WHITENOISE_AUTOREFRESH = True
    

    在你的settings.py。

    【讨论】:

      【解决方案3】:

      原来是一个 png 文件导致了这个问题。我不知道为什么 png 文件有时会以大写字母保存,例如 PNG。显然,这导致了白噪声问题。我花了很长时间才弄明白,希望对您有所帮助。

      【讨论】:

        【解决方案4】:

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

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

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

        并尝试在没有 / 的情况下获取 http://127.0.0.1:8000/admin。

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

        【讨论】:

          【解决方案5】:

          对我来说,static 模板标签存在问题,但白鼻子确实为我提供了有关问题/错误的任何有用信息。

          静态文件地址有误:

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

          应该是

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

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

          【讨论】:

            猜你喜欢
            • 2015-09-15
            • 2023-03-20
            • 2022-09-27
            • 1970-01-01
            • 2017-02-20
            • 2023-03-30
            • 2022-12-08
            • 1970-01-01
            • 2020-09-11
            相关资源
            最近更新 更多