Fixes #2966 -- Added extra_context parameter to direct_to_template generic view to keep it aligned with capabilities of other generic views. Thanks, wam-djangobug@wamber.net.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@3950 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2006-10-30 14:30:43 +00:00
parent 5ec32a1c2d
commit 6d1335c058
3 changed files with 17 additions and 3 deletions

View File

@ -160,6 +160,7 @@ answer newbie questions, and generally made Django that much better:
Amit Upadhyay Amit Upadhyay
Geert Vanderkelen Geert Vanderkelen
Milton Waddams Milton Waddams
wam-djangobug@wamber.net
Dan Watson <http://theidioteque.net/> Dan Watson <http://theidioteque.net/>
Rachel Willmer <http://www.willmer.com/kb/> Rachel Willmer <http://www.willmer.com/kb/>
Gary Wilson <gary.wilson@gmail.com> Gary Wilson <gary.wilson@gmail.com>

View File

@ -2,12 +2,18 @@ from django.shortcuts import render_to_response
from django.template import RequestContext from django.template import RequestContext
from django.http import HttpResponse, HttpResponsePermanentRedirect, HttpResponseGone from django.http import HttpResponse, HttpResponsePermanentRedirect, HttpResponseGone
def direct_to_template(request, template, **kwargs): def direct_to_template(request, template, extra_context={}, **kwargs):
""" """
Render a given template with any extra URL parameters in the context as Render a given template with any extra URL parameters in the context as
``{{ params }}``. ``{{ params }}``.
""" """
return render_to_response(template, {'params' : kwargs}, context_instance=RequestContext(request)) dictionary = {'params': kwargs}
for key, value in extra_context.items():
if callable(value):
dictionary[key] = value()
else:
dictionary[key] = value
return render_to_response(template, dictionary, context_instance=RequestContext(request))
def redirect_to(request, url, **kwargs): def redirect_to(request, url, **kwargs):
""" """

View File

@ -92,6 +92,13 @@ which is a dictionary of the parameters captured in the URL.
* ``template``: The full name of a template to use. * ``template``: The full name of a template to use.
**Optional arguments:**
* ``extra_context``: A dictionary of values to add to the template
context. By default, this is an empty dictionary. If a value in the
dictionary is callable, the generic view will call it
just before rendering the template.
**Example:** **Example:**
Given the following URL patterns:: Given the following URL patterns::
@ -171,7 +178,7 @@ a date in the *future* are not included unless you set ``allow_future`` to
template. By default, it's ``django.template.loader``. template. By default, it's ``django.template.loader``.
* ``extra_context``: A dictionary of values to add to the template * ``extra_context``: A dictionary of values to add to the template
context. By default, this is an empty dictionary. context. By default, this is an empty dictionary. If a value in the
dictionary is callable, the generic view will call it dictionary is callable, the generic view will call it
just before rendering the template. just before rendering the template.