我想编写一个自定义视图来渲染我的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()
派生了
的IndexViewclass 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内部错误。
有什么想法吗?
答案 0 :(得分:0)
不需要任何东西,小心你的定义,它们需要与原件相同。
class PageNotFoundView(TemplateView):
template_name = 'blogengine/404.html'
def get_context_data(self, **kwargs):
context = super(PageNotFoundView, self).get_context_data(**kwargs)
try:
context['categories'] = Category.objects.all()
except:
pass
return context