Python 定制手柄404=>;标准错误页
当我尝试使用自己的视图处理错误404时,我会得到一个标准的错误页面。那么,我现在做的是:Python 定制手柄404=>;标准错误页,python,django,Python,Django,当我尝试使用自己的视图处理错误404时,我会得到一个标准的错误页面。那么,我现在做的是: # root settings.py DEBUG = False ALLOWED_HOSTS = [ '*' ] # blog urls.py handler404 = 'views.custom_404' # blog views.py def custom_404(request): return HttpResponse("Hello world, I'm a custom err
# root settings.py
DEBUG = False
ALLOWED_HOSTS = [
'*'
]
# blog urls.py
handler404 = 'views.custom_404'
# blog views.py
def custom_404(request):
return HttpResponse("Hello world, I'm a custom error 404")
此外,为了在Django
测试服务器上本地尝试它,我运行它如下:
python manage.py runserver --insecure
但当我进入某个不存在的页面时,我得到的是:
Not Found
The requested url blablabla
因此,由于某些原因,我没有看到我自己的消息
你好,我是一个自定义错误404
404处理程序不是每个应用程序,它们只能从主URL.py设置。谢谢!我现在检查一下。我遇到了另一个问题。如果我只是显示一条消息,那就没关系了。但是,如果我尝试使用位于博客/templates
中的e-template页面404.html
,则会出现服务器错误500。你能告诉我如何解决这个问题吗?PS.settings.py知道blog/templates
路径,并且该文件夹中有一个真正的404.html
文件。我尝试如下方式渲染404.html
:return render\u to\u response('404.html',context\u实例=RequestContext(request))