Ticket #6603: extra_context-r7720.diff

File extra_context-r7720.diff, 9.0 KB (added by Adi J. Sieker, 16 years ago)

updated the patch. changed docs to reflect that QuerySet are now cloned. current tests pas.

  • docs/generic_views.txt

     
    6666to pass any auxiliary information you wish to the view. The values in the
    6767``extra_context`` dictionary can be either functions (or other callables) or
    6868other objects. Functions are evaluated just before they are passed to the
    69 template. However, note that QuerySets retrieve and cache their data when they
    70 are first evaluated, so if you want to pass in a QuerySet via
    71 ``extra_context`` that is always fresh you need to wrap it in a function or
    72 lambda that returns the QuerySet.
     69template. QuerySets will be cloned so that they are always fresh and up to date.
    7370
    7471.. _database API docs: ../db-api/
    7572
  • django/views/generic/common.py

     
     1from django.db.models.query import QuerySet
     2
     3def _add_extra_context(c, extra_context):
     4    for key, value in extra_context.items():
     5        if callable(value):
     6            c[key] = value()
     7        elif isinstance(value, QuerySet):
     8            c[key] = value._clone()
     9        else:
     10            c[key] = value
  • django/views/generic/create_update.py

     
    77from django.http import Http404, HttpResponse, HttpResponseRedirect
    88from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured
    99from django.utils.translation import ugettext
     10from django.views.generic.common import _add_extra_context
    1011
    1112def create_object(request, model, template_name=None,
    1213        template_loader=loader, extra_context=None, post_save_redirect=None,
     
    6364    c = RequestContext(request, {
    6465        'form': form,
    6566    }, context_processors)
    66     for key, value in extra_context.items():
    67         if callable(value):
    68             c[key] = value()
    69         else:
    70             c[key] = value
     67    _add_extra_context(c, extra_context)
    7168    return HttpResponse(t.render(c))
    7269
    7370def update_object(request, model, object_id=None, slug=None,
     
    136133        'form': form,
    137134        template_object_name: object,
    138135    }, context_processors)
    139     for key, value in extra_context.items():
    140         if callable(value):
    141             c[key] = value()
    142         else:
    143             c[key] = value
     136    _add_extra_context(c, extra_context)
    144137    response = HttpResponse(t.render(c))
    145138    populate_xheaders(request, response, model, getattr(object, object._meta.pk.attname))
    146139    return response
     
    190183        c = RequestContext(request, {
    191184            template_object_name: object,
    192185        }, context_processors)
    193         for key, value in extra_context.items():
    194             if callable(value):
    195                 c[key] = value()
    196             else:
    197                 c[key] = value
     186        _add_extra_context(c, extra_context)
    198187        response = HttpResponse(t.render(c))
    199188        populate_xheaders(request, response, model, getattr(object, object._meta.pk.attname))
    200189        return response
  • django/views/generic/date_based.py

     
    66from django.core.xheaders import populate_xheaders
    77from django.db.models.fields import DateTimeField
    88from django.http import Http404, HttpResponse
     9from django.views.generic.common import _add_extra_context
    910
    1011def archive_index(request, queryset, date_field, num_latest=15,
    1112        template_name=None, template_loader=loader,
     
    4142        'date_list' : date_list,
    4243        template_object_name : latest,
    4344    }, context_processors)
    44     for key, value in extra_context.items():
    45         if callable(value):
    46             c[key] = value()
    47         else:
    48             c[key] = value
     45    _add_extra_context(c, extra_context)
    4946    return HttpResponse(t.render(c), mimetype=mimetype)
    5047
    5148def archive_year(request, year, queryset, date_field, template_name=None,
     
    8986        'year': year,
    9087        '%s_list' % template_object_name: object_list,
    9188    }, context_processors)
    92     for key, value in extra_context.items():
    93         if callable(value):
    94             c[key] = value()
    95         else:
    96             c[key] = value
     89    _add_extra_context(c, extra_context)
    9790    return HttpResponse(t.render(c), mimetype=mimetype)
    9891
    9992def archive_month(request, year, month, queryset, date_field,
     
    155148        'next_month': next_month,
    156149        'previous_month': first_day - datetime.timedelta(days=1),
    157150    }, context_processors)
    158     for key, value in extra_context.items():
    159         if callable(value):
    160             c[key] = value()
    161         else:
    162             c[key] = value
     151    _add_extra_context(c, extra_context)
    163152    return HttpResponse(t.render(c), mimetype=mimetype)
    164153
    165154def archive_week(request, year, week, queryset, date_field,
     
    203192        '%s_list' % template_object_name: object_list,
    204193        'week': date,
    205194    })
    206     for key, value in extra_context.items():
    207         if callable(value):
    208             c[key] = value()
    209         else:
    210             c[key] = value
     195    _add_extra_context(c, extra_context)
    211196    return HttpResponse(t.render(c), mimetype=mimetype)
    212197
    213198def archive_day(request, year, month, day, queryset, date_field,
     
    267252        'previous_day': date - datetime.timedelta(days=1),
    268253        'next_day': next_day,
    269254    }, context_processors)
    270     for key, value in extra_context.items():
    271         if callable(value):
    272             c[key] = value()
    273         else:
    274             c[key] = value
     255    _add_extra_context(c, extra_context)
    275256    return HttpResponse(t.render(c), mimetype=mimetype)
    276257
    277258def archive_today(request, **kwargs):
     
    336317    c = RequestContext(request, {
    337318        template_object_name: obj,
    338319    }, context_processors)
    339     for key, value in extra_context.items():
    340         if callable(value):
    341             c[key] = value()
    342         else:
    343             c[key] = value
     320    _add_extra_context(c, extra_context)
    344321    response = HttpResponse(t.render(c), mimetype=mimetype)
    345322    populate_xheaders(request, response, model, getattr(obj, obj._meta.pk.name))
    346323    return response
  • django/views/generic/list_detail.py

     
    33from django.core.xheaders import populate_xheaders
    44from django.core.paginator import QuerySetPaginator, InvalidPage
    55from django.core.exceptions import ObjectDoesNotExist
     6from django.views.generic.common import _add_extra_context
    67
    78def object_list(request, queryset, paginate_by=None, page=None,
    89        allow_empty=True, template_name=None, template_loader=loader,
     
    8990        }, context_processors)
    9091        if not allow_empty and len(queryset) == 0:
    9192            raise Http404
    92     for key, value in extra_context.items():
    93         if callable(value):
    94             c[key] = value()
    95         else:
    96             c[key] = value
     93    _add_extra_context(c, extra_context)
    9794    if not template_name:
    9895        model = queryset.model
    9996        template_name = "%s/%s_list.html" % (model._meta.app_label, model._meta.object_name.lower())
     
    135132    c = RequestContext(request, {
    136133        template_object_name: obj,
    137134    }, context_processors)
    138     for key, value in extra_context.items():
    139         if callable(value):
    140             c[key] = value()
    141         else:
    142             c[key] = value
     135    _add_extra_context(c, extra_context)
    143136    response = HttpResponse(t.render(c), mimetype=mimetype)
    144137    populate_xheaders(request, response, model, getattr(obj, obj._meta.pk.name))
    145138    return response
  • django/views/generic/simple.py

     
    11from django.shortcuts import render_to_response
    22from django.template import loader, RequestContext
    33from django.http import HttpResponse, HttpResponsePermanentRedirect, HttpResponseGone
     4from django.views.generic.common import _add_extra_context
    45
    56def direct_to_template(request, template, extra_context=None, mimetype=None, **kwargs):
    67    """
     
    910    """
    1011    if extra_context is None: extra_context = {}
    1112    dictionary = {'params': kwargs}
    12     for key, value in extra_context.items():
    13         if callable(value):
    14             dictionary[key] = value()
    15         else:
    16             dictionary[key] = value
     13    _add_extra_context(dictionary, extra_context)
    1714    c = RequestContext(request, dictionary)
    1815    t = loader.get_template(template)
    1916    return HttpResponse(t.render(c), mimetype=mimetype)
Back to Top