Django 2.0的自定义错误页面 [英] Custom error page with django 2.0

查看:53
本文介绍了Django 2.0的自定义错误页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

here is my code in 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'

我错过了什么?

编辑bis整个代码:

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上运行。

so here is the whole urls.py file, it seems that my post is mostly code so i have to add more details like the fact this is the django 2.0 version or it runs on python3.6 on a ubuntu os.

推荐答案

如果您查看文档,此处仅使用一个位置参数定义了500处理程序。

If you take a look at the docs, there it defines the 500 handler with just one positional argument.

从以下位置更改代码

def handler500(request, exception):

def handler500(request):

让我们知道是否可行。

只是一个提示:自定义错误视图建议将视图函数放在 views.py 中,并仅添加字符串名称到您的 urls.py ,例如:

Just a hint: django docs on customizing error views suggests placing your view functions in views.py and just adding the string name to your urls.py, like:

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

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

这篇关于Django 2.0的自定义错误页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆