Skip to content

Commit

Permalink
Fixes #2966 -- Added extra_context parameter to direct_to_template ge…
Browse files Browse the repository at this point in the history
…neric 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
  • Loading branch information
russellm committed Oct 30, 2006
1 parent 6b3bda5 commit bdf3238
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Expand Up @@ -160,6 +160,7 @@ answer newbie questions, and generally made Django that much better:
Amit Upadhyay
Geert Vanderkelen
Milton Waddams
wam-djangobug@wamber.net
Dan Watson <http://theidioteque.net/>
Rachel Willmer <http://www.willmer.com/kb/>
Gary Wilson <gary.wilson@gmail.com>
Expand Down
10 changes: 8 additions & 2 deletions django/views/generic/simple.py
Expand Up @@ -2,12 +2,18 @@
from django.template import RequestContext
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
``{{ 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):
"""
Expand Down
9 changes: 8 additions & 1 deletion docs/generic_views.txt
Expand Up @@ -92,6 +92,13 @@ which is a dictionary of the parameters captured in the URL.

* ``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:**

Given the following URL patterns::
Expand Down Expand Up @@ -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``.

* ``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
just before rendering the template.

Expand Down

0 comments on commit bdf3238

Please sign in to comment.