zubhav
zubhav

Reputation: 1559

Django Custom 404 handler

We've recently upgraded to Django 1.10 and we're getting this error on the live site:

builtins:TypeError custom_404() got an unexpected keyword argument 'exception'

The code is as follows:

urls.py

urlpatterns = [ ... ]
handler404 = global_views.custom_404
handler500 = global_views.custom_500

global_views.py

def custom_404(request, exception, template_name='404.html'):
    return page_not_found(request, exception, template_name=template_name)   

def custom_500(request, template_name='500.html'):
    return server_error(request, template_name=template_name) 

We've tried many variations of this view but all result in that error.

What's going wrong?

Upvotes: 0

Views: 1458

Answers (2)

po5i
po5i

Reputation: 558

Your function should be defined as:

def custom_404(request, exception=None, template_name='404.html'):

Take me hours to figure this out.

Upvotes: 6

Alasdair
Alasdair

Reputation: 309089

The server_error view should not take exception as an argument. Its signature is

defaults.server_error(request, template_name='500.html')

It's not clear why you have defined a custom_404 view if all you do is call page_not_found. And calling page_not_found in you custom_500 view is very odd.

Upvotes: 1

Related Questions