简体   繁体   English

我在 Django 中看不到我的 404 和 500 自定义页面

[英]I can't see my 404 and 500 custom pages in Django

I am trying to make custom 404 and 500 pages in my Django project, but when I use these codes I can't see any result!我正在尝试在我的 Django 项目中制作自定义 404 和 500 页,但是当我使用这些代码时,我看不到任何结果!

views.py:视图.py:

def handle404(request,exception):
    return render(request,'400.html',status=404)

def handle500(request):
    return render(request,'500.html',status=500)


url.py :网址.py:

handler404 ='base.views.handle404'
handler500 = 'base.views.handle500'


i see only this page : enter image description here我只看到这个页面:在此处输入图片描述

anybody have some suggestions to improve my code ?有人有一些改进我的代码的建议吗?

Have you created the 400.html in your templates?您是否在模板中创建了400.html By that, I mean you follow standard Django patterns: app_name/templates/app_name/400.html ?我的意思是你遵循标准的 Django 模式: app_name/templates/app_name/400.html

(By the way, I would strongly recommend, that you keep the names matching to the errors. That way you can fast navigate in your HTMLs and you don't have to remember what HTML handles which error) (顺便说一句,我强烈建议您保持名称与错误匹配。这样您可以在 HTML 中快速导航,而不必记住 HTML 处理哪个错误)

UPDATE更新

You have to enter a specific path to the HTML into the render function.您必须在渲染函数中输入 HTML 的特定路径。

Meaning: If your error HTML lives in the app test_app your path would be test_app/400.html :含义:如果您的错误 HTML 存在于应用程序test_app中,您的路径将是test_app/400.html

def handle404(request,exception):
    return render(request,'test_app/400.html',status=404)

Not sure if you have set DEBUG=False in your settings.py file.不确定您是否在settings.py文件中设置了DEBUG=False

But this worked for me:但这对我有用:

Within my app.views :在我的app.views

# Handler error page 404
def handler404(request, exception):
     return render(request, '404.html', {})


# Handler error page 500
def handler500(request):
     return render(request, '500.html', {})

In the return statement , I have passed an empty context, {} .return statement ,我传递了一个空上下文{}

Within my project's urls.py file:在我的项目的urls.py文件中:

handler404 = 'app.views.handler404'
handler500 = 'app.views.handler500'

setting.py file: setting.py文件:

DEBUG=False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM