Anonymous avatar Anonymous committed 98a0cf3

Added django.views.generic.simple.redirect_to view for issuing simple redirects. Also updated direct_to_template to use render_to_response to be consistant with coding style, and documented the simple generic views.

Comments (0)

Files changed (2)

django/views/generic/simple.py

-from django.core import template_loader
-from django.core.extensions import DjangoContext
-from django.utils.httpwrappers import HttpResponse
+from django.core.extensions import DjangoContext, render_to_response
+from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect, HttpResponseGone
 
 def direct_to_template(request, template, **kwargs):
-    """Render a given template with any extra parameters in the context."""
-    t = template_loader.get_template(template)
-    c = DjangoContext(request, {'params' : kwargs})
-    return HttpResponse(t.render(c))
+    """
+    Render a given template with any extra URL parameters in the context as 
+    ``{{ params }}``.
+    """
+    return render_to_response(template, {'params' : kwargs}, context_instance=DjangoContext(request))
+    
+def redirect_to(request, url, **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 urlpattern::
+
+        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 url is not None:
+        return HttpResponseRedirect(url % kwargs)
+    else:
+        return HttpResponseGone()

docs/generic_views.txt

 ``year``, etc. do above) or from the additional-information dictionary (as for
 ``app_label``, ``module_name``, etc.).
 
-All the generic views that follow require the ``app_label`` and ``module_name`` keys.
-These values are easiest to explain through example::
+Most of the generic views that follow require the ``app_label`` and
+``module_name`` keys. These values are easiest to explain through example::
 
     >>> from django.models.blog import entries
 
 of the ``module_name`` option of your model). In the docs below, these keys
 will not be repeated, but each generic view requires them.
 
+Using "simple" generic views
+============================
+
+The ``django.views.generic.simple`` module contains simple views to handle a
+couple of common cases: rendering a template when no view logic is needed,
+and issuing a redirect.  These views are:
+
+``direct_to_template``
+    Renders a given template using any extra parameters passed in the
+    urlpattern; requires the ``template`` argument.
+    
+    For example, given the following URL patterns::
+        
+        urlpatterns = patterns('django.views.generic.simple',
+            (r'^foo/$',             'direct_to_template', {'template' : 'foo_index'}),
+            (r'^foo/(?P<id>\d+)/$', 'direct_to_template', {'template' : 'foo_detail'}),
+        )
+        
+    ... a request to ``/foo/`` would cause the ``foo_index`` template to be
+    rendered, and a request to ``/foo/15/`` would cause the ``foo_detail``
+    template to be rendered with a context variable ``{{ params.id }}`` that is
+    set to ``15``.
+    
+``redirect_to``
+    Issue a 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 urlpattern::
+
+        urlpatterns = patterns('django.views.generic.simple',
+            ('^foo/(?p<id>\d+)/$', 'redirect_to', {'url' : '/bar/%(id)s/'}),
+        )
+        
+    If the given url is ``None``, a HttpResponseGone (410) will be issued.
+
 Using date-based generic views
 ==============================
 
 
         object
             The object about to be deleted
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.