diff --git a/django/views/generic/create_update.py b/django/views/generic/create_update.py index 35ce31713d..f90891f0da 100644 --- a/django/views/generic/create_update.py +++ b/django/views/generic/create_update.py @@ -8,7 +8,7 @@ from django.utils.httpwrappers import HttpResponse, HttpResponseRedirect from django.core.exceptions import Http404, ObjectDoesNotExist, ImproperlyConfigured def create_object(request, app_label, module_name, template_name=None, - extra_context=None, post_save_redirect=None, login_required=False): + extra_context={}, post_save_redirect=None, login_required=False): """ Generic object-creation function. @@ -57,13 +57,16 @@ def create_object(request, app_label, module_name, template_name=None, c = Context(request, { 'form' : form, }) - if extra_context: - c.update(extra_context) + for key, value in extra_context.items(): + if callable(value): + c[key] = value() + else: + c[key] = value return HttpResponse(t.render(c)) def update_object(request, app_label, module_name, object_id=None, slug=None, slug_field=None, template_name=None, extra_lookup_kwargs={}, - extra_context=None, post_save_redirect=None, login_required=False): + extra_context={}, post_save_redirect=None, login_required=False): """ Generic object-update function. @@ -125,15 +128,18 @@ def update_object(request, app_label, module_name, object_id=None, slug=None, 'form' : form, 'object' : object, }) - if extra_context: - c.update(extra_context) + for key, value in extra_context.items(): + if callable(value): + c[key] = value() + else: + c[key] = value response = HttpResponse(t.render(c)) populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name)) return response def delete_object(request, app_label, module_name, post_delete_redirect, object_id=None, slug=None, slug_field=None, template_name=None, - extra_lookup_kwargs={}, extra_context=None, login_required=False): + extra_lookup_kwargs={}, extra_context={}, login_required=False): """ Generic object-delete function. @@ -177,8 +183,11 @@ def delete_object(request, app_label, module_name, post_delete_redirect, c = Context(request, { 'object' : object, }) - if extra_context: - c.update(extra_context) + for key, value in extra_context.items(): + if callable(value): + c[key] = value() + else: + c[key] = value response = HttpResponse(t.render(c)) populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name)) return response diff --git a/django/views/generic/date_based.py b/django/views/generic/date_based.py index 80ac2647a1..0e5e4bf5d1 100644 --- a/django/views/generic/date_based.py +++ b/django/views/generic/date_based.py @@ -7,7 +7,7 @@ from django.utils.httpwrappers import HttpResponse import datetime, time def archive_index(request, app_label, module_name, date_field, num_latest=15, - template_name=None, extra_lookup_kwargs={}, extra_context=None): + template_name=None, extra_lookup_kwargs={}, extra_context={}): """ Generic top-level archive of date-based objects. @@ -41,12 +41,15 @@ def archive_index(request, app_label, module_name, date_field, num_latest=15, 'date_list' : date_list, 'latest' : latest, }) - if extra_context: - c.update(extra_context) + for key, value in extra_context.items(): + if callable(value): + c[key] = value() + else: + c[key] = value return HttpResponse(t.render(c)) def archive_year(request, year, app_label, module_name, date_field, - template_name=None, extra_lookup_kwargs={}, extra_context=None): + template_name=None, extra_lookup_kwargs={}, extra_context={}): """ Generic yearly archive view. @@ -74,12 +77,15 @@ def archive_year(request, year, app_label, module_name, date_field, 'date_list': date_list, 'year': year, }) - if extra_context: - c.update(extra_context) + for key, value in extra_context.items(): + if callable(value): + c[key] = value() + else: + c[key] = value return HttpResponse(t.render(c)) def archive_month(request, year, month, app_label, module_name, date_field, - template_name=None, extra_lookup_kwargs={}, extra_context=None): + template_name=None, extra_lookup_kwargs={}, extra_context={}): """ Generic monthly archive view. @@ -121,12 +127,15 @@ def archive_month(request, year, month, app_label, module_name, date_field, 'object_list': object_list, 'month': date, }) - if extra_context: - c.update(extra_context) + for key, value in extra_context.items(): + if callable(value): + c[key] = value() + else: + c[key] = value return HttpResponse(t.render(c)) def archive_day(request, year, month, day, app_label, module_name, date_field, - template_name=None, extra_lookup_kwargs={}, extra_context=None, + template_name=None, extra_lookup_kwargs={}, extra_context={}, allow_empty=False): """ Generic daily archive view. @@ -167,8 +176,11 @@ def archive_day(request, year, month, day, app_label, module_name, date_field, 'previous_day': date - datetime.timedelta(days=1), 'next_day': (date < datetime.date.today()) and (date + datetime.timedelta(days=1)) or None, }) - if extra_context: - c.update(extra_context) + for key, value in extra_context.items(): + if callable(value): + c[key] = value() + else: + c[key] = value return HttpResponse(t.render(c)) def archive_today(request, **kwargs): @@ -185,7 +197,7 @@ def archive_today(request, **kwargs): def object_detail(request, year, month, day, app_label, module_name, date_field, object_id=None, slug=None, slug_field=None, template_name=None, - template_name_field=None, extra_lookup_kwargs={}, extra_context=None): + template_name_field=None, extra_lookup_kwargs={}, extra_context={}): """ Generic detail view from year/month/day/slug or year/month/day/id structure. @@ -227,8 +239,11 @@ def object_detail(request, year, month, day, app_label, module_name, date_field, c = Context(request, { 'object': object, }) - if extra_context: - c.update(extra_context) + for key, value in extra_context.items(): + if callable(value): + c[key] = value() + else: + c[key] = value response = HttpResponse(t.render(c)) populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name)) return response diff --git a/django/views/generic/list_detail.py b/django/views/generic/list_detail.py index 4723f254ab..1254eb93dd 100644 --- a/django/views/generic/list_detail.py +++ b/django/views/generic/list_detail.py @@ -7,7 +7,7 @@ from django.core.paginator import ObjectPaginator, InvalidPage from django.core.exceptions import Http404, ObjectDoesNotExist def object_list(request, app_label, module_name, paginate_by=None, allow_empty=False, - template_name=None, extra_lookup_kwargs={}, extra_context=None): + template_name=None, extra_lookup_kwargs={}, extra_context={}): """ Generic list of objects. @@ -61,8 +61,11 @@ def object_list(request, app_label, module_name, paginate_by=None, allow_empty=F }) if len(object_list) == 0 and not allow_empty: raise Http404 - if extra_context: - c.update(extra_context) + for key, value in extra_context.items(): + if callable(value): + c[key] = value() + else: + c[key] = value if not template_name: template_name = "%s/%s_list" % (app_label, module_name) t = template_loader.get_template(template_name) @@ -70,7 +73,7 @@ def object_list(request, app_label, module_name, paginate_by=None, allow_empty=F def object_detail(request, app_label, module_name, object_id=None, slug=None, slug_field=None, template_name=None, template_name_field=None, - extra_lookup_kwargs={}, extra_context=None): + extra_lookup_kwargs={}, extra_context={}): """ Generic list of objects. @@ -102,8 +105,11 @@ def object_detail(request, app_label, module_name, object_id=None, slug=None, c = Context(request, { 'object' : object, }) - if extra_context: - c.update(extra_context) + for key, value in extra_context.items(): + if callable(value): + c[key] = value() + else: + c[key] = value response = HttpResponse(t.render(c)) populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name)) return response