From 6d1335c0584c5a3b834915d00362427e4607755e Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Mon, 30 Oct 2006 14:30:43 +0000 Subject: [PATCH] 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 --- AUTHORS | 1 + django/views/generic/simple.py | 10 ++++++++-- docs/generic_views.txt | 9 ++++++++- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/AUTHORS b/AUTHORS index 923d6e6008..a605cb2105 100644 --- a/AUTHORS +++ b/AUTHORS @@ -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 Rachel Willmer Gary Wilson diff --git a/django/views/generic/simple.py b/django/views/generic/simple.py index 325288d52f..355bd25ef8 100644 --- a/django/views/generic/simple.py +++ b/django/views/generic/simple.py @@ -2,12 +2,18 @@ from django.shortcuts import render_to_response 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): """ diff --git a/docs/generic_views.txt b/docs/generic_views.txt index 99a9b7cf6b..f0c3f738f3 100644 --- a/docs/generic_views.txt +++ b/docs/generic_views.txt @@ -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:: @@ -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.