I have a website where some pages are edited by hand. When one of those templates is missing, it just means that the page is not present, so I would like to display an Error 404.
Instead I get an exception TemplateDoesNotExist.
Is there a way to tell Django to display an error 404 whenever it does not find a template?
If you want this behaviour for all views on your site, you might want to write your own middleware with a
process_exception
method.If you have defined your own
handler404
you would need to replacepage_not_found
above. I'm not immediately sure how you could convert the stringhandler404
into the callable required in the middleware..To enable your middleware, add it to
MIDDLEWARE_CLASSES
insettings.py
. Be careful of the position where you add it. The standard Django middleware warning applies:put the return of the response in the view (or whereever the template is rendered) in a try-except block:
Off the top of my head, but if you set DEBUG=False in your settings, won't you get a 404 then on every error (including TemplateNotFound)?