class CheckDoesNotExistsMiddleware(object): def process_exception(self, request, exception): if isinstance(exception, ObjectDoesNotExist): raise Http404 in settings.py MIDDLEWARE_CLASSES = ( ........... ........... 'gcrmserver.apps.core.middleware.CheckDoesNotExistsMiddleware', )