【发布时间】:2014-08-07 08:42:09
【问题描述】:
我想编写一个自定义视图来呈现我的 404。所以我这样定义它:
class PageNotFoundView(IndexView):
template_name = 'blogengine/404.html'
def get_queryset(self):
return Post.objects.none
def get_context_data(self):
return self.get_context_categories()
它派生的IndexView是
class IndexView(ListView):
def get_context_categories(self):
context = super(IndexView, self).get_context_data(**self.kwargs)
context['categories'] = Category.objects.all()
return context
因为我想要所有页面中的所有类别。
并在我的主 urlconf 中像这样设置 urlconf
from django.conf.urls import patterns, include, url
from django.contrib import admin
from blogengine.views import PageNotFoundView
admin.autodiscover()
urlpatterns = patterns('',
url(r'^admin/', include(admin.site.urls)),
url(r'', include('blogengine.urls')),
)
handler404 = PageNotFoundView.as_view()
在 settings.py 中我放置了我需要的主机:
DEBUG = False
TEMPLATE_DEBUG = False
ALLOWED_HOSTS = ['my.customHost.com', 'localhost', '127.0.0.1',]
但是当我部署它时,当我尝试错误的 URL 时出现 500 内部错误。
有什么想法吗?
【问题讨论】:
标签: django python-3.x http-status-code-404