Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
  • Loading branch information...
commit 58641f806804757e0e4f07d3e71681ee5f50cb4f 1 parent 9562cbb
@adrianholovaty adrianholovaty authored
Showing with 16 additions and 2 deletions.
  1. +16 −2 django/core/extensions.py
View
18 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
Please sign in to comment.
Something went wrong with that request. Please try again.