Django 1.9.2中的错误404处理程序

时间:2016-02-23 15:09:19

标签: python django

我已安装Django 1.9.2并希望使用自定义视图来处理错误404.我正在尝试这样做:

# myapp/urls.py

from django.conf.urls import url, handler404
from django.views.generic.base import RedirectView
from django.core.urlresolvers import reverse_lazy

import views


urlpatterns = [
    url(r'^not-found/$', views.NotFoundView.as_view(), name='not_found'),
]

#handler404 = RedirectView.as_view(url=reverse_lazy('not_found'))
handler404 = 'myapp.views.NotFoundView.not_found_handler'



# myapp/views/NotFoundView.py

from django.http import HttpResponse


class NotFoundView(object):
    @classmethod
    def as_view(cls):
        return cls.handler

    @classmethod
    def handler(cls, request):
        return HttpResponse(u"My error 404", status=404)


def not_found_handler(request, exception, template_name='404.html'):
    return HttpResponse(u"New handler 404", status=404)

换句话说,我将handler404的{​​{1}}覆盖为我的自定义视图RedirectView初始化的NotFoundView。但不幸的是它没有用,我得到了标准Django的错误404而不是我的自定义错误。我做错了什么?

0 个答案:

没有答案