Home > Back-end >  I can't see my 404 and 500 custom pages in Django
I can't see my 404 and 500 custom pages in Django

Time:06-22

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!

views.py:

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

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


url.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 ?

CodePudding user response:

Have you created the 400.html in your statics?

(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)

CodePudding user response:

Not sure if you have set DEBUG=False in your settings.py file.

But this worked for me:

Within my 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, {}.

Within my project's urls.py file:

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

setting.py file:

DEBUG=False
  • Related