Please explain "TypeError: handle_404() got an unexpected keyword argument 'exception'"

378 views
Skip to first unread message

Chris Seberino

unread,
Dec 11, 2017, 8:23:18 PM12/11/17
to Django users
I'm trying to get a 404 page to show instead of a 500 error when Django app sees a nonexistent URL.

I'm still getting 500 errors with this clue in logs but I don't know what it means....

Please explain "TypeError: handle_404() got an unexpected keyword argument 'exception'"

(The nonexistent URL I tried was /aaa.)

Internal Server Error: /aaa
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 113, in _get_response
    resolver_match = resolver.resolve(request.path_info)
  File "/usr/local/lib/python3.5/dist-packages/django/urls/resolvers.py", line 523, in resolve
    raise Resolver404({'tried': tried, 'path': new_path})
django.urls.exceptions.Resolver404: {'path': 'aaa', 'tried': [[<URLPattern '^$'>], [<URLPattern '^main/handle_404/$'>],
 [<URLPattern '^main/main/$'>], [<URLPattern '^explorer/search/$'>]]}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 99, in get_exception_response
    response = callback(request, **dict(param_dict, exception=exception))
TypeError: handle_404() got an unexpected keyword argument 'exception'


Matemática A3K

unread,
Dec 11, 2017, 11:43:35 PM12/11/17
to django...@googlegroups.com
On Mon, Dec 11, 2017 at 10:23 PM, Chris Seberino <cseb...@gmail.com> wrote:
I'm trying to get a 404 page to show instead of a 500 error when Django app sees a nonexistent URL.

I'm still getting 500 errors with this clue in logs but I don't know what it means....

Please explain "TypeError: handle_404() got an unexpected keyword argument 'exception'"

It means literally that "handle_404() got an unexpected keyword argument 'exception'", how are you calling handle_404()?
 

(The nonexistent URL I tried was /aaa.)

Internal Server Error: /aaa
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 35, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/base.py", line 113, in _get_response
    resolver_match = resolver.resolve(request.path_info)
  File "/usr/local/lib/python3.5/dist-packages/django/urls/resolvers.py", line 523, in resolve
    raise Resolver404({'tried': tried, 'path': new_path})
django.urls.exceptions.Resolver404: {'path': 'aaa', 'tried': [[<URLPattern '^$'>], [<URLPattern '^main/handle_404/$'>],
 [<URLPattern '^main/main/$'>], [<URLPattern '^explorer/search/$'>]]}

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/django/core/handlers/exception.py", line 99, in get_exception_response
    response = callback(request, **dict(param_dict, exception=exception))
TypeError: handle_404() got an unexpected keyword argument 'exception'


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c87198a3-327c-4dbf-a6d0-114a800fa560%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Chris Seberino

unread,
Dec 12, 2017, 12:27:03 PM12/12/17
to Django users


Please explain "TypeError: handle_404() got an unexpected keyword argument 'exception'"

It means literally that "handle_404() got an unexpected keyword argument 'exception'", how are you calling handle_404()?


Thanks for your help.  In my urls.py I have this...

django.conf.urls.handler404 = "main.controllers.controllers.handle_404"

I believe that invokes the handle_404 function in the main/controllers/controllers.py file?

cs 

Daniel Roseman

unread,
Dec 12, 2017, 5:09:28 PM12/12/17
to Django users
And what does your handle_404 function look like? As the error says, it needs to accept an "exception" keyword argument.

Note also that overriding the 404 handler is a very specialized thing to do and is almost always not required. Why are you doing this?
--
DR.

Chris Seberino

unread,
Dec 12, 2017, 7:09:01 PM12/12/17
to Django users


And what does your handle_404 function look like? As the error says, it needs to accept an "exception" keyword argument.

Note also that overriding the 404 handler is a very specialized thing to do and is almost always not required. Why are you doing this?


Here it is..... The date is just to provide the current date at bottom for the copyright footer.  Are you saying views like this
are rare?

 def handle_404(request):
        """
        Handles 404 errors.
        """

        year = datetime.date.today().year

        return django.shortcuts.render(request,
                                       "handle_404.html",
                                       {"year" : year})

Dylan Reinhold

unread,
Dec 12, 2017, 9:13:15 PM12/12/17
to django...@googlegroups.com
Your handle_404(request): needs to accept request, exception and template.

But if what you want this for is the current Year.
Just create a 404 template file in your template root named 404.html.
In the template use
{% now 'Y' %}
To get the current year.

The just let the default django 404 error process pick it up that as it's the default template to use for 404..

Make sure you have DEBUG=False for your testing.

Dylan

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages