Django

Code

Changeset 1249

Show
Ignore:
Timestamp:
11/15/05 11:19:33 (3 years ago)
Author:
jacob
Message:

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.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/views/generic/simple.py

    r1247 r1249  
    1 from django.core import template_loader 
    2 from django.core.extensions import DjangoContext 
    3 from django.utils.httpwrappers import HttpResponse 
     1from django.core.extensions import DjangoContext, render_to_response 
     2from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect, HttpResponseGone 
    43 
    54def 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     
     11def 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() 
  • django/trunk/docs/generic_views.txt

    r828 r1249  
    5858``app_label``, ``module_name``, etc.). 
    5959 
    60 All the generic views that follow require the ``app_label`` and ``module_name`` keys. 
    61 These values are easiest to explain through example:: 
     60Most of the generic views that follow require the ``app_label`` and 
     61``module_name`` keys. These values are easiest to explain through example:: 
    6262 
    6363    >>> from django.models.blog import entries 
     
    6868of the ``module_name`` option of your model). In the docs below, these keys 
    6969will not be repeated, but each generic view requires them. 
     70 
     71Using "simple" generic views 
     72============================ 
     73 
     74The ``django.views.generic.simple`` module contains simple views to handle a 
     75couple of common cases: rendering a template when no view logic is needed, 
     76and 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. 
    70106 
    71107Using date-based generic views 
     
    323359        object 
    324360            The object about to be deleted 
     361