web/lib/django/views/generic/simple.py
changeset 0 0d40e90630ef
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/web/lib/django/views/generic/simple.py	Wed Jan 20 00:34:04 2010 +0100
@@ -0,0 +1,41 @@
+from django.template import loader, RequestContext
+from django.http import HttpResponse, HttpResponseRedirect, HttpResponsePermanentRedirect, HttpResponseGone
+
+def direct_to_template(request, template, extra_context=None, mimetype=None, **kwargs):
+    """
+    Render a given template with any extra URL parameters in the context as
+    ``{{ params }}``.
+    """
+    if extra_context is None: extra_context = {}
+    dictionary = {'params': kwargs}
+    for key, value in extra_context.items():
+        if callable(value):
+            dictionary[key] = value()
+        else:
+            dictionary[key] = value
+    c = RequestContext(request, dictionary)
+    t = loader.get_template(template)
+    return HttpResponse(t.render(c), mimetype=mimetype)
+
+def redirect_to(request, url, permanent=True, **kwargs):
+    """
+    Redirect to a given URL.
+
+    The given url may contain dict-style string formatting, which will be
+    interpolated against the params in the URL.  For example, to redirect from
+    ``/foo/<id>/`` to ``/bar/<id>/``, you could use the following URLconf::
+
+        urlpatterns = patterns('',
+            ('^foo/(?P<id>\d+)/$', 'django.views.generic.simple.redirect_to', {'url' : '/bar/%(id)s/'}),
+        )
+
+    If the given url is ``None``, a HttpResponseGone (410) will be issued.
+
+    If the ``permanent`` argument is False, then the response will have a 302
+    HTTP status code. Otherwise, the status code will be 301.
+    """
+    if url is not None:
+        klass = permanent and HttpResponsePermanentRedirect or HttpResponseRedirect
+        return klass(url % kwargs)
+    else:
+        return HttpResponseGone()