Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1249 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 400cf5658da46fc748e64a7570a6df0dd21cbff2 1 parent dcb5bc3
Jacob Kaplan-Moss authored November 15, 2005
33  django/views/generic/simple.py
... ...
@@ -1,9 +1,28 @@
1  
-from django.core import template_loader
2  
-from django.core.extensions import DjangoContext
3  
-from django.utils.httpwrappers import HttpResponse
  1
+from django.core.extensions import DjangoContext, render_to_response
  2
+from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect, HttpResponseGone
4 3
 
5 4
 def direct_to_template(request, template, **kwargs):
6  
-    """Render a given template with any extra parameters in the context."""
7  
-    t = template_loader.get_template(template)
8  
-    c = DjangoContext(request, {'params' : kwargs})
9  
-    return HttpResponse(t.render(c))
  5
+    """
  6
+    Render a given template with any extra URL parameters in the context as 
  7
+    ``{{ params }}``.
  8
+    """
  9
+    return render_to_response(template, {'params' : kwargs}, context_instance=DjangoContext(request))
  10
+    
  11
+def redirect_to(request, url, **kwargs):
  12
+    """
  13
+    Redirect to a given URL.  
  14
+    
  15
+    The given url may contain dict-style string formatting which will be
  16
+    interpolated against the params in the URL.  For example, to redirect from
  17
+    ``/foo/<id>/`` to ``/bar/<id>/``, you could use the following urlpattern::
  18
+
  19
+        urlpatterns = patterns('',
  20
+            ('^foo/(?p<id>\d+)/$', 'django.views.generic.simple.redirect_to', {'url' : '/bar/%(id)s/'}),
  21
+        )
  22
+        
  23
+    If the given url is ``None``, a HttpResponseGone (410) will be issued.
  24
+    """
  25
+    if url is not None:
  26
+        return HttpResponseRedirect(url % kwargs)
  27
+    else:
  28
+        return HttpResponseGone()
41  docs/generic_views.txt
@@ -57,8 +57,8 @@ arguments may either come from the URL pattern (as ``month``, ``day``,
57 57
 ``year``, etc. do above) or from the additional-information dictionary (as for
58 58
 ``app_label``, ``module_name``, etc.).
59 59
 
60  
-All the generic views that follow require the ``app_label`` and ``module_name`` keys.
61  
-These values are easiest to explain through example::
  60
+Most of the generic views that follow require the ``app_label`` and
  61
+``module_name`` keys. These values are easiest to explain through example::
62 62
 
63 63
     >>> from django.models.blog import entries
64 64
 
@@ -68,6 +68,42 @@ holds all your model definitions) and ``entries`` is the ``module_name``
68 68
 of the ``module_name`` option of your model). In the docs below, these keys
69 69
 will not be repeated, but each generic view requires them.
70 70
 
  71
+Using "simple" generic views
  72
+============================
  73
+
  74
+The ``django.views.generic.simple`` module contains simple views to handle a
  75
+couple of common cases: rendering a template when no view logic is needed,
  76
+and issuing a redirect.  These views are:
  77
+
  78
+``direct_to_template``
  79
+    Renders a given template using any extra parameters passed in the
  80
+    urlpattern; requires the ``template`` argument.
  81
+    
  82
+    For example, given the following URL patterns::
  83
+        
  84
+        urlpatterns = patterns('django.views.generic.simple',
  85
+            (r'^foo/$',             'direct_to_template', {'template' : 'foo_index'}),
  86
+            (r'^foo/(?P<id>\d+)/$', 'direct_to_template', {'template' : 'foo_detail'}),
  87
+        )
  88
+        
  89
+    ... a request to ``/foo/`` would cause the ``foo_index`` template to be
  90
+    rendered, and a request to ``/foo/15/`` would cause the ``foo_detail``
  91
+    template to be rendered with a context variable ``{{ params.id }}`` that is
  92
+    set to ``15``.
  93
+    
  94
+``redirect_to``
  95
+    Issue a redirect to a given URL.
  96
+
  97
+    The given url may contain dict-style string formatting which will be
  98
+    interpolated against the params in the URL.  For example, to redirect from
  99
+    ``/foo/<id>/`` to ``/bar/<id>/``, you could use the following urlpattern::
  100
+
  101
+        urlpatterns = patterns('django.views.generic.simple',
  102
+            ('^foo/(?p<id>\d+)/$', 'redirect_to', {'url' : '/bar/%(id)s/'}),
  103
+        )
  104
+        
  105
+    If the given url is ``None``, a HttpResponseGone (410) will be issued.
  106
+
71 107
 Using date-based generic views
72 108
 ==============================
73 109
 
@@ -322,3 +358,4 @@ The create/update/delete views are:
322 358
 
323 359
         object
324 360
             The object about to be deleted
  361
+

0 notes on commit 400cf56

Please sign in to comment.
Something went wrong with that request. Please try again.