Changeset 1249
- Timestamp:
- 11/15/05 11:19:33 (3 years ago)
- Files:
-
- django/trunk/django/views/generic/simple.py (modified) (1 diff)
- django/trunk/docs/generic_views.txt (modified) (3 diffs)
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 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() django/trunk/docs/generic_views.txt
r828 r1249 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 … … 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 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. 70 106 71 107 Using date-based generic views … … 323 359 object 324 360 The object about to be deleted 361
