From 58641f806804757e0e4f07d3e71681ee5f50cb4f Mon Sep 17 00:00:00 2001 From: Adrian Holovaty Date: Fri, 23 Sep 2005 21:40:04 +0000 Subject: [PATCH] Added django.core.extensions get_object_or_404 and get_list_or_404 git-svn-id: http://code.djangoproject.com/svn/django/trunk@673 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/extensions.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/django/core/extensions.py b/django/core/extensions.py index 6cbd1ebe28..6909ace33d 100644 --- a/django/core/extensions.py +++ b/django/core/extensions.py @@ -1,15 +1,29 @@ -# Specialized template classes for Django, decoupled from the basic template system. +# This module collects helper functions and classes that "span" multiple levels +# of MVC. In other words, these functions/classes introduce controlled coupling +# for convenience's sake. from django.core import template_loader +from django.core.exceptions import Http404, ObjectDoesNotExist from django.core.template import Context from django.conf.settings import DEBUG, INTERNAL_IPS from django.utils.httpwrappers import HttpResponse def render_to_response(*args, **kwargs): return HttpResponse(template_loader.render_to_string(*args, **kwargs)) - load_and_render = render_to_response # For backwards compatibility. +def get_object_or_404(mod, **kwargs): + try: + return mod.get_object(**kwargs) + except ObjectDoesNotExist: + raise Http404 + +def get_list_or_404(mod, **kwargs): + obj_list = mod.get_list(**kwargs) + if not obj_list: + raise Http404 + return obj_list + class DjangoContext(Context): """ This subclass of template.Context automatically populates 'user' and