0

When Debug = True django returns a Page not found (404) page which is totally fine

But when I change Debug = False django return a Server Error (500) and the server shows these requests made:

[30/Jul/2022 12:29:44] "GET /__reload__/events/ HTTP/1.1" 500 145
[30/Jul/2022 12:29:44] "GET /__reload__/events/ HTTP/1.1" 500 145
[30/Jul/2022 12:29:44] "GET /sdfs HTTP/1.1" 500 145
[30/Jul/2022 12:29:45] "GET /favicon.ico HTTP/1.1" 302 0

myproject main urls.py

from django.conf.urls import handler404
handler404 = 'myproject.views.custom_page_not_found'

please note here that hander404 is not found by django handler404 not found

my views.py

def custom_page_not_found(request, exception, template_name="errors/404.html"):
    return render(request, template_name, status=404)

Please not here django cannot find the exception parameter exception parameter not found

In previous community questions i could not find any question that points to the exception parameter not found issue. Please note that i am not using django templates {% %} in my 404.html file

I think when django does not find the exception parameter it returns server error 500.

1 Answers1

0

you don't need to import this handler from django.conf.urls import handler404.

you should simply to define variable in url:

handler404 = 'myproject.views.custom_page_not_found'

as function of view (is better) you can made this:

def custom_page_not_found(request, *args, template_name="errors/404.html", **kargs):
    return render(request, template_name, status=404)
Maxim Danilov
  • 2,472
  • 1
  • 3
  • 8
  • I tried with `*args, **kargs` instead of `exception` as a parameter but it's not working and returns **server error 500** with the same log. – Zaeem Akhtar Jul 30 '22 at 17:10
  • When i return the function with `return HttpResponse('404', status=404)` it works. But i don't understand why it is not working when the function is return with `return render(request, template_name, status=404)` – Zaeem Akhtar Jul 30 '22 at 17:28
  • Hey, thank God it works when `None` is passed as a request to render `return render(None, 'errors/404.html', status=404)` but i have no idea why django does this?. – Zaeem Akhtar Jul 30 '22 at 17:58
  • These posts are helpful https://stackoverflow.com/questions/55911435/django-importerror-cannot-import-name-render-to-response-from-django-shortcu & https://codehunter.cc/a/django/custom-django-404-error – Zaeem Akhtar Jul 30 '22 at 18:15