Skip to content

Instantly share code, notes, and snippets.

@victorono
Created February 12, 2014 13:33
Show Gist options
  • Save victorono/8955620 to your computer and use it in GitHub Desktop.
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.
#!/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