module documentation

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.
Function get​_list​_or​_404 Use filter() to return a list of objects, or raise an Http404 exception if the list is empty.
Function get​_object​_or​_404 Use get() to return an object, or raise an Http404 exception if the object does not exist.
Function redirect Return an HttpResponseRedirect to the appropriate URL for the arguments passed.
Function render Return an HttpResponse whose content is filled with the result of calling django.template.loader.render_to_string() with the passed arguments.
Function ​_get​_queryset No summary
Function resolve​_url Return a URL appropriate for the arguments passed.
def get_list_or_404(klass, *args, **kwargs):

Use filter() to return a list of objects, or raise an Http404 exception if the list is empty.

klass may be a Model, Manager, or QuerySet object. All other passed arguments and keyword arguments are used in the filter() query.

def get_object_or_404(klass, *args, **kwargs):

Use get() to return an object, or raise an Http404 exception if the object does not exist.

klass may be a Model, Manager, or QuerySet object. All other passed arguments and keyword arguments are used in the get() query.

Like with QuerySet.get(), MultipleObjectsReturned is raised if more than one object is found.

def redirect(to, *args, permanent=False, **kwargs):

Return an HttpResponseRedirect to the appropriate URL for the arguments passed.

The arguments could be:

  • A model: the model's get_absolute_url() function will be called.
  • A view name, possibly with arguments: urls.reverse() will be used to reverse-resolve the name.
  • A URL, which will be used as-is for the redirect location.

Issues a temporary redirect by default; pass permanent=True to issue a permanent redirect.

def render(request, template_name, context=None, content_type=None, status=None, using=None):
Return an HttpResponse whose content is filled with the result of calling django.template.loader.render_to_string() with the passed arguments.
def _get_queryset(klass):
Return a QuerySet or a Manager. Duck typing in action: any class with a get() method (for get_object_or_404) or a filter() method (for get_list_or_404) might do the job.
def resolve_url(to, *args, **kwargs):

Return a URL appropriate for the arguments passed.

The arguments could be:

  • A model: the model's get_absolute_url() function will be called.
  • A view name, possibly with arguments: urls.reverse() will be used to reverse-resolve the name.
  • A URL, which will be returned as-is.