Created
February 12, 2014 13:33
-
-
Save victorono/8955620 to your computer and use it in GitHub Desktop.
A Middleware that can be applied to your views to turn ObjectDoesNotExist exceptions into Http404 exceptions. This means people will see a "Page not found" error rather than an "Internal Server Error" when they are request something that does not exist in the database.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from django.core.exceptions import ObjectDoesNotExist | |
from django.http import Http404 | |
class DoesNotExistTo404Middleware(object): | |
def process_exception(self, request, exception): | |
if isinstance(exception,ObjectDoesNotExist): | |
raise Http404(exception.message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment