Previous Page Next Page

Using Simple Generic Views

The first two generic views we will look at are the simple generic views django.views.generic.simple.direct_to_template and django.views.generic.simple.redirect_to.

direct_to_template

The direct_to_template view allows you to render a template file directly from the URLconf without using a view function.

The following list describes the required arguments, optional arguments, and context variables available when using the direct_to_template view (see Tables 12.1 and 12.2 for information about the arguments and context variables):

The following is a sample URL pattern for the direct_to_template view:

(r'^(?P<num>\d+/$', 'direct_to_template', { 'template' : 'index.html'}),

Any arguments passed in from the URL are available in the template from the params context variable. For example, you could use the following line of code to access the num argument from the preceding URL pattern:

{{ params.num }}

redirect_to

The redirect_to view allows you to redirect the HTTP request to another URL. This can be useful if you want to support old URLs or redirect several URLs to a specific view.

For example, the following URL pattern redirects /files/2008 to /arch/2008:

(r'^files/(?P<year>\d{4}/$', 'redirect_to', { 'url' : '/arch/%(year)'}),

The following list describes the required arguments, optional arguments, and context variables available when using the redirect_to view (see Tables 12.1 and 12.2 for information about the arguments and context variables):

Previous Page Next Page