Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.5.x] Fixed #19076 -- Added content_type attribute to TemplateView.

Thanks Gavin Wahl.

Backport of 23e319d.
  • Loading branch information...
commit d5ad9d3dfe729f8b605965e131c787fae9f84377 1 parent 5da6ce1
Aymeric Augustin authored
2  django/views/generic/base.py
@@ -113,6 +113,7 @@ class TemplateResponseMixin(object):
113 113
     """
114 114
     template_name = None
115 115
     response_class = TemplateResponse
  116
+    content_type = None
116 117
 
117 118
     def render_to_response(self, context, **response_kwargs):
118 119
         """
@@ -122,6 +123,7 @@ def render_to_response(self, context, **response_kwargs):
122 123
         If any keyword arguments are provided, they will be
123 124
         passed to the constructor of the response class.
124 125
         """
  126
+        response_kwargs.setdefault('content_type', self.content_type)
125 127
         return self.response_class(
126 128
             request = self.request,
127 129
             template = self.get_template_names(),
14  docs/ref/class-based-views/flattened-index.txt
@@ -32,6 +32,7 @@ TemplateView
32 32
 
33 33
 **Attributes** (with optional accessor):
34 34
 
  35
+* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
35 36
 * :attr:`~django.views.generic.base.View.http_method_names`
36 37
 * :attr:`~django.views.generic.base.TemplateResponseMixin.response_class`
37 38
 * :attr:`~django.views.generic.base.TemplateResponseMixin.template_name` [:meth:`~django.views.generic.base.TemplateResponseMixin.get_template_names`]
@@ -79,6 +80,7 @@ DetailView
79 80
 
80 81
 **Attributes** (with optional accessor):
81 82
 
  83
+* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
82 84
 * :attr:`~django.views.generic.detail.SingleObjectMixin.context_object_name` [:meth:`~django.views.generic.detail.SingleObjectMixin.get_context_object_name`]
83 85
 * :attr:`~django.views.generic.base.View.http_method_names`
84 86
 * :attr:`~django.views.generic.detail.SingleObjectMixin.model`
@@ -112,6 +114,7 @@ ListView
112 114
 **Attributes** (with optional accessor):
113 115
 
114 116
 * :attr:`~django.views.generic.list.MultipleObjectMixin.allow_empty` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_allow_empty`]
  117
+* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
115 118
 * :attr:`~django.views.generic.list.MultipleObjectMixin.context_object_name` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_context_object_name`]
116 119
 * :attr:`~django.views.generic.base.View.http_method_names`
117 120
 * :attr:`~django.views.generic.list.MultipleObjectMixin.model`
@@ -143,6 +146,7 @@ FormView
143 146
 
144 147
 **Attributes** (with optional accessor):
145 148
 
  149
+* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
146 150
 * :attr:`~django.views.generic.edit.FormMixin.form_class` [:meth:`~django.views.generic.edit.FormMixin.get_form_class`]
147 151
 * :attr:`~django.views.generic.base.View.http_method_names`
148 152
 * :attr:`~django.views.generic.edit.FormMixin.initial` [:meth:`~django.views.generic.edit.FormMixin.get_initial`]
@@ -172,6 +176,7 @@ CreateView
172 176
 
173 177
 **Attributes** (with optional accessor):
174 178
 
  179
+* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
175 180
 * :attr:`~django.views.generic.detail.SingleObjectMixin.context_object_name` [:meth:`~django.views.generic.detail.SingleObjectMixin.get_context_object_name`]
176 181
 * :attr:`~django.views.generic.edit.FormMixin.form_class` [:meth:`~django.views.generic.edit.FormMixin.get_form_class`]
177 182
 * :attr:`~django.views.generic.base.View.http_method_names`
@@ -210,6 +215,7 @@ UpdateView
210 215
 
211 216
 **Attributes** (with optional accessor):
212 217
 
  218
+* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
213 219
 * :attr:`~django.views.generic.detail.SingleObjectMixin.context_object_name` [:meth:`~django.views.generic.detail.SingleObjectMixin.get_context_object_name`]
214 220
 * :attr:`~django.views.generic.edit.FormMixin.form_class` [:meth:`~django.views.generic.edit.FormMixin.get_form_class`]
215 221
 * :attr:`~django.views.generic.base.View.http_method_names`
@@ -248,6 +254,7 @@ DeleteView
248 254
 
249 255
 **Attributes** (with optional accessor):
250 256
 
  257
+* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
251 258
 * :attr:`~django.views.generic.detail.SingleObjectMixin.context_object_name` [:meth:`~django.views.generic.detail.SingleObjectMixin.get_context_object_name`]
252 259
 * :attr:`~django.views.generic.base.View.http_method_names`
253 260
 * :attr:`~django.views.generic.detail.SingleObjectMixin.model`
@@ -285,6 +292,7 @@ ArchiveIndexView
285 292
 
286 293
 * :attr:`~django.views.generic.list.MultipleObjectMixin.allow_empty` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_allow_empty`]
287 294
 * :attr:`~django.views.generic.dates.DateMixin.allow_future` [:meth:`~django.views.generic.dates.DateMixin.get_allow_future`]
  295
+* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
288 296
 * :attr:`~django.views.generic.list.MultipleObjectMixin.context_object_name` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_context_object_name`]
289 297
 * :attr:`~django.views.generic.dates.DateMixin.date_field` [:meth:`~django.views.generic.dates.DateMixin.get_date_field`]
290 298
 * :attr:`~django.views.generic.base.View.http_method_names`
@@ -319,6 +327,7 @@ YearArchiveView
319 327
 
320 328
 * :attr:`~django.views.generic.list.MultipleObjectMixin.allow_empty` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_allow_empty`]
321 329
 * :attr:`~django.views.generic.dates.DateMixin.allow_future` [:meth:`~django.views.generic.dates.DateMixin.get_allow_future`]
  330
+* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
322 331
 * :attr:`~django.views.generic.list.MultipleObjectMixin.context_object_name` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_context_object_name`]
323 332
 * :attr:`~django.views.generic.dates.DateMixin.date_field` [:meth:`~django.views.generic.dates.DateMixin.get_date_field`]
324 333
 * :attr:`~django.views.generic.base.View.http_method_names`
@@ -356,6 +365,7 @@ MonthArchiveView
356 365
 
357 366
 * :attr:`~django.views.generic.list.MultipleObjectMixin.allow_empty` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_allow_empty`]
358 367
 * :attr:`~django.views.generic.dates.DateMixin.allow_future` [:meth:`~django.views.generic.dates.DateMixin.get_allow_future`]
  368
+* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
359 369
 * :attr:`~django.views.generic.list.MultipleObjectMixin.context_object_name` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_context_object_name`]
360 370
 * :attr:`~django.views.generic.dates.DateMixin.date_field` [:meth:`~django.views.generic.dates.DateMixin.get_date_field`]
361 371
 * :attr:`~django.views.generic.base.View.http_method_names`
@@ -396,6 +406,7 @@ WeekArchiveView
396 406
 
397 407
 * :attr:`~django.views.generic.list.MultipleObjectMixin.allow_empty` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_allow_empty`]
398 408
 * :attr:`~django.views.generic.dates.DateMixin.allow_future` [:meth:`~django.views.generic.dates.DateMixin.get_allow_future`]
  409
+* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
399 410
 * :attr:`~django.views.generic.list.MultipleObjectMixin.context_object_name` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_context_object_name`]
400 411
 * :attr:`~django.views.generic.dates.DateMixin.date_field` [:meth:`~django.views.generic.dates.DateMixin.get_date_field`]
401 412
 * :attr:`~django.views.generic.base.View.http_method_names`
@@ -434,6 +445,7 @@ DayArchiveView
434 445
 
435 446
 * :attr:`~django.views.generic.list.MultipleObjectMixin.allow_empty` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_allow_empty`]
436 447
 * :attr:`~django.views.generic.dates.DateMixin.allow_future` [:meth:`~django.views.generic.dates.DateMixin.get_allow_future`]
  448
+* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
437 449
 * :attr:`~django.views.generic.list.MultipleObjectMixin.context_object_name` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_context_object_name`]
438 450
 * :attr:`~django.views.generic.dates.DateMixin.date_field` [:meth:`~django.views.generic.dates.DateMixin.get_date_field`]
439 451
 * :attr:`~django.views.generic.dates.DayMixin.day` [:meth:`~django.views.generic.dates.DayMixin.get_day`]
@@ -478,6 +490,7 @@ TodayArchiveView
478 490
 
479 491
 * :attr:`~django.views.generic.list.MultipleObjectMixin.allow_empty` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_allow_empty`]
480 492
 * :attr:`~django.views.generic.dates.DateMixin.allow_future` [:meth:`~django.views.generic.dates.DateMixin.get_allow_future`]
  493
+* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
481 494
 * :attr:`~django.views.generic.list.MultipleObjectMixin.context_object_name` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_context_object_name`]
482 495
 * :attr:`~django.views.generic.dates.DateMixin.date_field` [:meth:`~django.views.generic.dates.DateMixin.get_date_field`]
483 496
 * :attr:`~django.views.generic.dates.DayMixin.day` [:meth:`~django.views.generic.dates.DayMixin.get_day`]
@@ -521,6 +534,7 @@ DateDetailView
521 534
 **Attributes** (with optional accessor):
522 535
 
523 536
 * :attr:`~django.views.generic.dates.DateMixin.allow_future` [:meth:`~django.views.generic.dates.DateMixin.get_allow_future`]
  537
+* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
524 538
 * :attr:`~django.views.generic.detail.SingleObjectMixin.context_object_name` [:meth:`~django.views.generic.detail.SingleObjectMixin.get_context_object_name`]
525 539
 * :attr:`~django.views.generic.dates.DateMixin.date_field` [:meth:`~django.views.generic.dates.DateMixin.get_date_field`]
526 540
 * :attr:`~django.views.generic.dates.DayMixin.day` [:meth:`~django.views.generic.dates.DayMixin.get_day`]
9  docs/ref/class-based-views/mixins-simple.txt
@@ -64,6 +64,15 @@ TemplateResponseMixin
64 64
         instantiation, create a ``TemplateResponse`` subclass and assign it to
65 65
         ``response_class``.
66 66
 
  67
+    .. attribute:: content_type
  68
+
  69
+        .. versionadded:: 1.5
  70
+            The ``content_type`` attribute was added.
  71
+
  72
+        The content type to use for the response. ``content_type`` is passed
  73
+        as a keyword argument to ``response_class``. Default is ``None`` --
  74
+        meaning that Django uses :setting:`DEFAULT_CONTENT_TYPE`.
  75
+
67 76
     **Methods**
68 77
 
69 78
     .. method:: render_to_response(context, **response_kwargs)
5  tests/regressiontests/generic_views/base.py
@@ -312,6 +312,11 @@ def test_cached_views(self):
312 312
 
313 313
         self.assertNotEqual(response.content, response2.content)
314 314
 
  315
+    def test_content_type(self):
  316
+        response = self.client.get('/template/content_type/')
  317
+        self.assertEqual(response['Content-Type'], 'text/plain')
  318
+
  319
+
315 320
 class RedirectViewTest(unittest.TestCase):
316 321
     rf = RequestFactory()
317 322
 
2  tests/regressiontests/generic_views/urls.py
@@ -20,6 +20,8 @@
20 20
         TemplateView.as_view(template_name='generic_views/about.html')),
21 21
     (r'^template/custom/(?P<foo>\w+)/$',
22 22
         views.CustomTemplateView.as_view(template_name='generic_views/about.html')),
  23
+    (r'^template/content_type/$',
  24
+        TemplateView.as_view(template_name='generic_views/robots.txt', content_type='text/plain')),
23 25
 
24 26
     (r'^template/cached/(?P<foo>\w+)/$',
25 27
         cache_page(2.0)(TemplateView.as_view(template_name='generic_views/about.html'))),

0 notes on commit d5ad9d3

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