[英]Django returning 500 Internal Server Error instead of index.html when Debug = False
我的 django web 应用程序出现问题,其中 django 在尝试获取 index.django 时返回 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'
终端中出现的错误
"GET / HTTP/1.1" 500 145
网址.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")),
),
]
视图.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
模板标签存在问题,但白鼻子确实为我提供了有关问题/错误的任何有用信息。
static 文件的地址有错误:
<link rel="icon" href="{% static 'images/favicon.png' %}">
应该是
<link rel="icon" href="{% static 'blog/images/favicon.png' %}">
更正地址后,它解决了我的问题。
我最近遇到了这个问题,我不得不删除
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
并将其替换为
STATICFILES_STORAGE = 'django.contrib.staticfiles.storage.StaticFilesStorage'
我希望这有帮助。
祝你今天过得愉快
您可以将STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
更改为STATICFILES_STORAGE = 'whitenoise.storage.CompressedStaticFilesStorage'
。
您将在提供文件时保持压缩,但您将丢失 static 文件文件名的散列。
whitenoise.storage.CompressedManifestStaticFilesStorage
所做的是在collectstatic
方法期间将 static 文件作为具有唯一散列名称的附加文件保存到您的STATIC_ROOT
目录中。
当您使用 whitenoise 为您的 static 文件提供服务时,它会在浏览器中以 4 小时的默认 Cache-Control 缓存文件。 这意味着您页面的访问者,当他们第一次访问您的网站时,会获得 static 文件,如果他们在这 4 小时内再次访问 window,他们将使用 ZA81259CEF8E959C3624DF1D456E 文件的缓存版本。 这导致更快的页面加载时间。
您可以将此缓存控制设置为更长的时间(例如 1 年),但这意味着您对 static 文件所做的任何更新,如果用户仍然在浏览器中获得 static 文件的缓存版本,则用户将无法看到.
Django 通过ManifestStaticFilesStorage
提供了一种解决方法。 它使用 hash 生成的文件名保存文件,该文件名仅在文件更新时才会更改。 Whitenoise 将这些文件视为新文件,并在它们发生更改时在浏览器缓存中更新它们。 你基本上得到了两个世界的最好的:更长的缓存控制和用户浏览器如果他们被改变了新的文件。
出于某种原因,django 没有读取您的散列文件名。 这与白噪声无关,是 django "django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
问题。 首先尝试让它在没有白噪声的情况下工作,并弄清楚为什么"django.contrib.staticfiles.storage.ManifestStaticFilesStorage"
会引发 500 服务器错误。
我个人有同样的问题,无法在任何地方找到解决方案。 我决定保持压缩并将 static 文件的保质期保留为默认 4 小时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.