tags:

views:

35

answers:

1

I'm creating a Django app and trying to have it touch the containing project in as few places as possible.

I've created a custom 404 view which works as expected but only when I add handler404 to the project-level urls.py.

I want the custom 404 view that I've written to apply to this particular app only, but from the information I've come across it appears that this may not be possible. Adding handler404 to the app-level urls.py does not have any effect.

Does Django support custom 404 views at the application level?

A: 

Instead of finishing your view with:

return render_to_response(...)

Get the response object and check its status:

out = render_to_response(...)

if out.status_code==404:
    # redirect to your 404 page
else:
    return out
joel3000
Turn that into a decorator for reuse.
joel3000
This is not entirely appropriate in my case, as I'm raising `django.http.Http404` myself. Reading your answer made me realize that I can simply call my own `page_not_found` view when required, and can ensure that it is used if none of the other URL patterns match by appending `(r'', page_not_found),` to my `urlpatterns` tuple. Thanks for spurring my brain into gear. :)
davidchambers