Django 2.0的自定义错误页面

时间:2018-08-01 18:15:18

标签: django python-3.x

这是我在urls.py中的代码:

def handler500(request, exception):
    response =  HttpResponseServerError('error.html', {},
                                  context_instance=RequestContext(request))
    response.status_code = 500
    return response

但是我有一个:

TypeError: handler500() missing 1 required positional argument: 'exception'

我想念什么?

编辑整个代码

from django.contrib import admin
from django.urls import path
from django.urls import include

from django.conf import settings
from django.conf.urls.static import static
from django.http import HttpResponseServerError

urlpatterns = [path("admin/", admin.site.urls)]
urlpatterns += [path("", include("Exchange.urls"))]

urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)


from django.shortcuts import render_to_response
from django.template import RequestContext


def handler404(request, exception, template_name='404.html'):
    response = render_to_response('404.html', {},
                                  context_instance=RequestContext(request))
    response.status_code = 404
    return response


def handler500(request,exception):
    response =  HttpResponseServerError('error.html', {},
                                  context_instance=RequestContext(request))
    response.status_code = 500
    return response

所以这是整个urls.py文件,似乎我的帖子主要是代码,所以我必须添加更多详细信息,例如这是django 2.0版本或它在ubuntu os上的python3.6上运行。 / p>

1 个答案:

答案 0 :(得分:2)

如果您看一看docs,它会使用一个位置参数定义500处理程序。

更改您的代码

def handler500(request, exception):

def handler500(request):

让我们知道是否可行。


只是一个提示:customizing error views上的django docs建议将视图函数放在views.py中,只需将字符串名称添加到urls.py中,例如:

# in views.py
def handler500(request):
    ...

# in urls.py
handler500 = 'mysite.views.handler500'