Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #195: generic views now allow callables in extra_context dicts …

…-- thanks, Moof!

git-svn-id: http://code.djangoproject.com/svn/django/trunk@307 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 6c43b0e06c09b7311aeba850a9e5ca3596f62e96 1 parent f6c4395
Jacob Kaplan-Moss authored July 25, 2005
27  django/views/generic/create_update.py
@@ -8,7 +8,7 @@
8 8
 from django.core.exceptions import Http404, ObjectDoesNotExist, ImproperlyConfigured
9 9
 
10 10
 def create_object(request, app_label, module_name, template_name=None, 
11  
-                 extra_context=None, post_save_redirect=None, login_required=False):
  11
+                 extra_context={}, post_save_redirect=None, login_required=False):
12 12
     """
13 13
     Generic object-creation function.
14 14
     
@@ -57,13 +57,16 @@ def create_object(request, app_label, module_name, template_name=None,
57 57
     c = Context(request, {
58 58
         'form' : form,
59 59
     })
60  
-    if extra_context:
61  
-        c.update(extra_context)
  60
+    for key, value in extra_context.items():
  61
+        if callable(value):
  62
+            c[key] = value()
  63
+        else:   
  64
+            c[key] = value
62 65
     return HttpResponse(t.render(c))
63 66
 
64 67
 def update_object(request, app_label, module_name, object_id=None, slug=None, 
65 68
                   slug_field=None, template_name=None, extra_lookup_kwargs={}, 
66  
-                  extra_context=None, post_save_redirect=None, login_required=False):
  69
+                  extra_context={}, post_save_redirect=None, login_required=False):
67 70
     """
68 71
     Generic object-update function.
69 72
 
@@ -125,15 +128,18 @@ def update_object(request, app_label, module_name, object_id=None, slug=None,
125 128
         'form' : form,
126 129
         'object' : object,
127 130
     })
128  
-    if extra_context:
129  
-        c.update(extra_context)
  131
+    for key, value in extra_context.items():
  132
+        if callable(value):
  133
+            c[key] = value()
  134
+        else:   
  135
+            c[key] = value
130 136
     response = HttpResponse(t.render(c))
131 137
     populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))
132 138
     return response
133 139
 
134 140
 def delete_object(request, app_label, module_name, post_delete_redirect, 
135 141
                   object_id=None, slug=None, slug_field=None, template_name=None, 
136  
-                  extra_lookup_kwargs={}, extra_context=None, login_required=False):
  142
+                  extra_lookup_kwargs={}, extra_context={}, login_required=False):
137 143
     """
138 144
     Generic object-delete function.
139 145
     
@@ -177,8 +183,11 @@ def delete_object(request, app_label, module_name, post_delete_redirect,
177 183
         c = Context(request, {
178 184
             'object' : object,
179 185
         })
180  
-        if extra_context:
181  
-            c.update(extra_context)
  186
+        for key, value in extra_context.items():
  187
+            if callable(value):
  188
+                c[key] = value()
  189
+            else:   
  190
+                c[key] = value
182 191
         response = HttpResponse(t.render(c))
183 192
         populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))
184 193
         return response
45  django/views/generic/date_based.py
@@ -7,7 +7,7 @@
7 7
 import datetime, time
8 8
 
9 9
 def archive_index(request, app_label, module_name, date_field, num_latest=15, 
10  
-                  template_name=None, extra_lookup_kwargs={}, extra_context=None):
  10
+                  template_name=None, extra_lookup_kwargs={}, extra_context={}):
11 11
     """
12 12
     Generic top-level archive of date-based objects.
13 13
 
@@ -41,12 +41,15 @@ def archive_index(request, app_label, module_name, date_field, num_latest=15,
41 41
         'date_list' : date_list,
42 42
         'latest' : latest,
43 43
     })
44  
-    if extra_context:
45  
-        c.update(extra_context)
  44
+    for key, value in extra_context.items():
  45
+        if callable(value):
  46
+            c[key] = value()
  47
+        else:   
  48
+            c[key] = value
46 49
     return HttpResponse(t.render(c))
47 50
 
48 51
 def archive_year(request, year, app_label, module_name, date_field, 
49  
-                 template_name=None, extra_lookup_kwargs={}, extra_context=None):
  52
+                 template_name=None, extra_lookup_kwargs={}, extra_context={}):
50 53
     """
51 54
     Generic yearly archive view.
52 55
 
@@ -74,12 +77,15 @@ def archive_year(request, year, app_label, module_name, date_field,
74 77
         'date_list': date_list,
75 78
         'year': year,
76 79
     })
77  
-    if extra_context:
78  
-        c.update(extra_context)
  80
+    for key, value in extra_context.items():
  81
+        if callable(value):
  82
+            c[key] = value()
  83
+        else:   
  84
+            c[key] = value
79 85
     return HttpResponse(t.render(c))
80 86
 
81 87
 def archive_month(request, year, month, app_label, module_name, date_field, 
82  
-                  template_name=None, extra_lookup_kwargs={}, extra_context=None):
  88
+                  template_name=None, extra_lookup_kwargs={}, extra_context={}):
83 89
     """
84 90
     Generic monthly archive view.
85 91
 
@@ -121,12 +127,15 @@ def archive_month(request, year, month, app_label, module_name, date_field,
121 127
         'object_list': object_list,
122 128
         'month': date,
123 129
     })
124  
-    if extra_context:
125  
-        c.update(extra_context)
  130
+    for key, value in extra_context.items():
  131
+        if callable(value):
  132
+            c[key] = value()
  133
+        else:   
  134
+            c[key] = value
126 135
     return HttpResponse(t.render(c))
127 136
 
128 137
 def archive_day(request, year, month, day, app_label, module_name, date_field, 
129  
-                template_name=None, extra_lookup_kwargs={}, extra_context=None, 
  138
+                template_name=None, extra_lookup_kwargs={}, extra_context={}, 
130 139
                 allow_empty=False):
131 140
     """
132 141
     Generic daily archive view.
@@ -167,8 +176,11 @@ def archive_day(request, year, month, day, app_label, module_name, date_field,
167 176
         'previous_day': date - datetime.timedelta(days=1),
168 177
         'next_day': (date < datetime.date.today()) and (date + datetime.timedelta(days=1)) or None,
169 178
     })
170  
-    if extra_context:
171  
-        c.update(extra_context)
  179
+    for key, value in extra_context.items():
  180
+        if callable(value):
  181
+            c[key] = value()
  182
+        else:   
  183
+            c[key] = value
172 184
     return HttpResponse(t.render(c))
173 185
 
174 186
 def archive_today(request, **kwargs):
@@ -185,7 +197,7 @@ def archive_today(request, **kwargs):
185 197
 
186 198
 def object_detail(request, year, month, day, app_label, module_name, date_field, 
187 199
                   object_id=None, slug=None, slug_field=None, template_name=None, 
188  
-                  template_name_field=None, extra_lookup_kwargs={}, extra_context=None):
  200
+                  template_name_field=None, extra_lookup_kwargs={}, extra_context={}):
189 201
     """
190 202
     Generic detail view from year/month/day/slug or year/month/day/id structure.
191 203
 
@@ -227,8 +239,11 @@ def object_detail(request, year, month, day, app_label, module_name, date_field,
227 239
     c = Context(request, {
228 240
         'object': object,
229 241
     })
230  
-    if extra_context:
231  
-        c.update(extra_context)
  242
+    for key, value in extra_context.items():
  243
+        if callable(value):
  244
+            c[key] = value()
  245
+        else:   
  246
+            c[key] = value
232 247
     response = HttpResponse(t.render(c))
233 248
     populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))
234 249
     return response
18  django/views/generic/list_detail.py
@@ -7,7 +7,7 @@
7 7
 from django.core.exceptions import Http404, ObjectDoesNotExist
8 8
 
9 9
 def object_list(request, app_label, module_name, paginate_by=None, allow_empty=False, 
10  
-                template_name=None, extra_lookup_kwargs={}, extra_context=None):
  10
+                template_name=None, extra_lookup_kwargs={}, extra_context={}):
11 11
     """
12 12
     Generic list of objects.
13 13
 
@@ -61,8 +61,11 @@ def object_list(request, app_label, module_name, paginate_by=None, allow_empty=F
61 61
         })
62 62
     if len(object_list) == 0 and not allow_empty:
63 63
         raise Http404
64  
-    if extra_context:
65  
-        c.update(extra_context)
  64
+    for key, value in extra_context.items():
  65
+        if callable(value):
  66
+            c[key] = value()
  67
+        else:   
  68
+            c[key] = value
66 69
     if not template_name:
67 70
         template_name = "%s/%s_list" % (app_label, module_name)
68 71
     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
70 73
 
71 74
 def object_detail(request, app_label, module_name, object_id=None, slug=None, 
72 75
                   slug_field=None, template_name=None, template_name_field=None, 
73  
-                  extra_lookup_kwargs={}, extra_context=None):
  76
+                  extra_lookup_kwargs={}, extra_context={}):
74 77
     """
75 78
     Generic list of objects.
76 79
 
@@ -102,8 +105,11 @@ def object_detail(request, app_label, module_name, object_id=None, slug=None,
102 105
     c = Context(request, {
103 106
         'object' : object,
104 107
     })
105  
-    if extra_context:
106  
-        c.update(extra_context)
  108
+    for key, value in extra_context.items():
  109
+        if callable(value):
  110
+            c[key] = value()
  111
+        else:   
  112
+            c[key] = value
107 113
     response = HttpResponse(t.render(c))
108 114
     populate_xheaders(request, response, app_label, module_name, getattr(object, object._meta.pk.name))
109 115
     return response

0 notes on commit 6c43b0e

Please sign in to comment.
Something went wrong with that request. Please try again.