I have production server which has these settings:
DEBUG = False
ALLOWED_HOSTS = ['localhost', '127.0.0.1', 'my.ip.address', 'example.com']
Then in my main urls.py file I have added this:
from django.conf.urls import url, include, handler404, handler500
handler500 = 'main.views.error_500'
handler404 = 'main.views.error_404'
My main.views has these:
def error_404(request):
return render(request, 'main/errors/404.html', status=404)
def error_500(request):
return render(request, 'main/errors/500.html', status=500)
When I deploy my production server and try to open page that does not exists, it should redirect to my error_404 view. However, it redirects to my error_500 view. Why this is happening?
This question is three months old but for the community this is how I solved this:
The 404-page-not-found-view needs an "exception" argument:
def error_404(request, exception):
return render(request,'myapp/404.html', status = 404)
The default 404 view will pass two variables to the template: request_path, which is the URL that resulted in the error, and exception, which is a useful representation of the exception that triggered the view (e.g. containing any message passed to a specific Http404 instance).
Source: https://docs.djangoproject.com/en/2.1/ref/views/#the-404-page-not-found-view
I'm Django-noob but I suppose that without this argument the 404 trigger a 500.
One more thing: It works for me without the import : "from django.conf.urls import handler404, handler500"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With