Ticket #6603: extra_context-r7720.diff
File extra_context-r7720.diff, 9.0 KB (added by , 16 years ago) |
---|
-
docs/generic_views.txt
66 66 to pass any auxiliary information you wish to the view. The values in the 67 67 ``extra_context`` dictionary can be either functions (or other callables) or 68 68 other 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. 69 template. QuerySets will be cloned so that they are always fresh and up to date. 73 70 74 71 .. _database API docs: ../db-api/ 75 72 -
django/views/generic/common.py
1 from django.db.models.query import QuerySet 2 3 def _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
7 7 from django.http import Http404, HttpResponse, HttpResponseRedirect 8 8 from django.core.exceptions import ObjectDoesNotExist, ImproperlyConfigured 9 9 from django.utils.translation import ugettext 10 from django.views.generic.common import _add_extra_context 10 11 11 12 def create_object(request, model, template_name=None, 12 13 template_loader=loader, extra_context=None, post_save_redirect=None, … … 63 64 c = RequestContext(request, { 64 65 'form': form, 65 66 }, 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) 71 68 return HttpResponse(t.render(c)) 72 69 73 70 def update_object(request, model, object_id=None, slug=None, … … 136 133 'form': form, 137 134 template_object_name: object, 138 135 }, 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) 144 137 response = HttpResponse(t.render(c)) 145 138 populate_xheaders(request, response, model, getattr(object, object._meta.pk.attname)) 146 139 return response … … 190 183 c = RequestContext(request, { 191 184 template_object_name: object, 192 185 }, 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) 198 187 response = HttpResponse(t.render(c)) 199 188 populate_xheaders(request, response, model, getattr(object, object._meta.pk.attname)) 200 189 return response -
django/views/generic/date_based.py
6 6 from django.core.xheaders import populate_xheaders 7 7 from django.db.models.fields import DateTimeField 8 8 from django.http import Http404, HttpResponse 9 from django.views.generic.common import _add_extra_context 9 10 10 11 def archive_index(request, queryset, date_field, num_latest=15, 11 12 template_name=None, template_loader=loader, … … 41 42 'date_list' : date_list, 42 43 template_object_name : latest, 43 44 }, 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) 49 46 return HttpResponse(t.render(c), mimetype=mimetype) 50 47 51 48 def archive_year(request, year, queryset, date_field, template_name=None, … … 89 86 'year': year, 90 87 '%s_list' % template_object_name: object_list, 91 88 }, 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) 97 90 return HttpResponse(t.render(c), mimetype=mimetype) 98 91 99 92 def archive_month(request, year, month, queryset, date_field, … … 155 148 'next_month': next_month, 156 149 'previous_month': first_day - datetime.timedelta(days=1), 157 150 }, 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) 163 152 return HttpResponse(t.render(c), mimetype=mimetype) 164 153 165 154 def archive_week(request, year, week, queryset, date_field, … … 203 192 '%s_list' % template_object_name: object_list, 204 193 'week': date, 205 194 }) 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) 211 196 return HttpResponse(t.render(c), mimetype=mimetype) 212 197 213 198 def archive_day(request, year, month, day, queryset, date_field, … … 267 252 'previous_day': date - datetime.timedelta(days=1), 268 253 'next_day': next_day, 269 254 }, 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) 275 256 return HttpResponse(t.render(c), mimetype=mimetype) 276 257 277 258 def archive_today(request, **kwargs): … … 336 317 c = RequestContext(request, { 337 318 template_object_name: obj, 338 319 }, 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) 344 321 response = HttpResponse(t.render(c), mimetype=mimetype) 345 322 populate_xheaders(request, response, model, getattr(obj, obj._meta.pk.name)) 346 323 return response -
django/views/generic/list_detail.py
3 3 from django.core.xheaders import populate_xheaders 4 4 from django.core.paginator import QuerySetPaginator, InvalidPage 5 5 from django.core.exceptions import ObjectDoesNotExist 6 from django.views.generic.common import _add_extra_context 6 7 7 8 def object_list(request, queryset, paginate_by=None, page=None, 8 9 allow_empty=True, template_name=None, template_loader=loader, … … 89 90 }, context_processors) 90 91 if not allow_empty and len(queryset) == 0: 91 92 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) 97 94 if not template_name: 98 95 model = queryset.model 99 96 template_name = "%s/%s_list.html" % (model._meta.app_label, model._meta.object_name.lower()) … … 135 132 c = RequestContext(request, { 136 133 template_object_name: obj, 137 134 }, 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) 143 136 response = HttpResponse(t.render(c), mimetype=mimetype) 144 137 populate_xheaders(request, response, model, getattr(obj, obj._meta.pk.name)) 145 138 return response -
django/views/generic/simple.py
1 1 from django.shortcuts import render_to_response 2 2 from django.template import loader, RequestContext 3 3 from django.http import HttpResponse, HttpResponsePermanentRedirect, HttpResponseGone 4 from django.views.generic.common import _add_extra_context 4 5 5 6 def direct_to_template(request, template, extra_context=None, mimetype=None, **kwargs): 6 7 """ … … 9 10 """ 10 11 if extra_context is None: extra_context = {} 11 12 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) 17 14 c = RequestContext(request, dictionary) 18 15 t = loader.get_template(template) 19 16 return HttpResponse(t.render(c), mimetype=mimetype)