Fixed #19076 -- Added content_type attribute to TemplateView.
Thanks Gavin Wahl.
This commit is contained in:
parent
3f1a0c0040
commit
23e319d729
|
@ -113,6 +113,7 @@ class TemplateResponseMixin(object):
|
|||
"""
|
||||
template_name = None
|
||||
response_class = TemplateResponse
|
||||
content_type = None
|
||||
|
||||
def render_to_response(self, context, **response_kwargs):
|
||||
"""
|
||||
|
@ -122,6 +123,7 @@ class TemplateResponseMixin(object):
|
|||
If any keyword arguments are provided, they will be
|
||||
passed to the constructor of the response class.
|
||||
"""
|
||||
response_kwargs.setdefault('content_type', self.content_type)
|
||||
return self.response_class(
|
||||
request = self.request,
|
||||
template = self.get_template_names(),
|
||||
|
|
|
@ -32,6 +32,7 @@ TemplateView
|
|||
|
||||
**Attributes** (with optional accessor):
|
||||
|
||||
* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
|
||||
* :attr:`~django.views.generic.base.View.http_method_names`
|
||||
* :attr:`~django.views.generic.base.TemplateResponseMixin.response_class`
|
||||
* :attr:`~django.views.generic.base.TemplateResponseMixin.template_name` [:meth:`~django.views.generic.base.TemplateResponseMixin.get_template_names`]
|
||||
|
@ -79,6 +80,7 @@ DetailView
|
|||
|
||||
**Attributes** (with optional accessor):
|
||||
|
||||
* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
|
||||
* :attr:`~django.views.generic.detail.SingleObjectMixin.context_object_name` [:meth:`~django.views.generic.detail.SingleObjectMixin.get_context_object_name`]
|
||||
* :attr:`~django.views.generic.base.View.http_method_names`
|
||||
* :attr:`~django.views.generic.detail.SingleObjectMixin.model`
|
||||
|
@ -112,6 +114,7 @@ ListView
|
|||
**Attributes** (with optional accessor):
|
||||
|
||||
* :attr:`~django.views.generic.list.MultipleObjectMixin.allow_empty` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_allow_empty`]
|
||||
* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
|
||||
* :attr:`~django.views.generic.list.MultipleObjectMixin.context_object_name` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_context_object_name`]
|
||||
* :attr:`~django.views.generic.base.View.http_method_names`
|
||||
* :attr:`~django.views.generic.list.MultipleObjectMixin.model`
|
||||
|
@ -144,6 +147,7 @@ FormView
|
|||
|
||||
**Attributes** (with optional accessor):
|
||||
|
||||
* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
|
||||
* :attr:`~django.views.generic.edit.FormMixin.form_class` [:meth:`~django.views.generic.edit.FormMixin.get_form_class`]
|
||||
* :attr:`~django.views.generic.base.View.http_method_names`
|
||||
* :attr:`~django.views.generic.edit.FormMixin.initial` [:meth:`~django.views.generic.edit.FormMixin.get_initial`]
|
||||
|
@ -173,6 +177,7 @@ CreateView
|
|||
|
||||
**Attributes** (with optional accessor):
|
||||
|
||||
* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
|
||||
* :attr:`~django.views.generic.detail.SingleObjectMixin.context_object_name` [:meth:`~django.views.generic.detail.SingleObjectMixin.get_context_object_name`]
|
||||
* :attr:`~django.views.generic.edit.FormMixin.form_class` [:meth:`~django.views.generic.edit.FormMixin.get_form_class`]
|
||||
* :attr:`~django.views.generic.base.View.http_method_names`
|
||||
|
@ -211,6 +216,7 @@ UpdateView
|
|||
|
||||
**Attributes** (with optional accessor):
|
||||
|
||||
* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
|
||||
* :attr:`~django.views.generic.detail.SingleObjectMixin.context_object_name` [:meth:`~django.views.generic.detail.SingleObjectMixin.get_context_object_name`]
|
||||
* :attr:`~django.views.generic.edit.FormMixin.form_class` [:meth:`~django.views.generic.edit.FormMixin.get_form_class`]
|
||||
* :attr:`~django.views.generic.base.View.http_method_names`
|
||||
|
@ -249,6 +255,7 @@ DeleteView
|
|||
|
||||
**Attributes** (with optional accessor):
|
||||
|
||||
* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
|
||||
* :attr:`~django.views.generic.detail.SingleObjectMixin.context_object_name` [:meth:`~django.views.generic.detail.SingleObjectMixin.get_context_object_name`]
|
||||
* :attr:`~django.views.generic.base.View.http_method_names`
|
||||
* :attr:`~django.views.generic.detail.SingleObjectMixin.model`
|
||||
|
@ -286,6 +293,7 @@ ArchiveIndexView
|
|||
|
||||
* :attr:`~django.views.generic.list.MultipleObjectMixin.allow_empty` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_allow_empty`]
|
||||
* :attr:`~django.views.generic.dates.DateMixin.allow_future` [:meth:`~django.views.generic.dates.DateMixin.get_allow_future`]
|
||||
* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
|
||||
* :attr:`~django.views.generic.list.MultipleObjectMixin.context_object_name` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_context_object_name`]
|
||||
* :attr:`~django.views.generic.dates.DateMixin.date_field` [:meth:`~django.views.generic.dates.DateMixin.get_date_field`]
|
||||
* :attr:`~django.views.generic.base.View.http_method_names`
|
||||
|
@ -321,6 +329,7 @@ YearArchiveView
|
|||
|
||||
* :attr:`~django.views.generic.list.MultipleObjectMixin.allow_empty` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_allow_empty`]
|
||||
* :attr:`~django.views.generic.dates.DateMixin.allow_future` [:meth:`~django.views.generic.dates.DateMixin.get_allow_future`]
|
||||
* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
|
||||
* :attr:`~django.views.generic.list.MultipleObjectMixin.context_object_name` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_context_object_name`]
|
||||
* :attr:`~django.views.generic.dates.DateMixin.date_field` [:meth:`~django.views.generic.dates.DateMixin.get_date_field`]
|
||||
* :attr:`~django.views.generic.base.View.http_method_names`
|
||||
|
@ -359,6 +368,7 @@ MonthArchiveView
|
|||
|
||||
* :attr:`~django.views.generic.list.MultipleObjectMixin.allow_empty` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_allow_empty`]
|
||||
* :attr:`~django.views.generic.dates.DateMixin.allow_future` [:meth:`~django.views.generic.dates.DateMixin.get_allow_future`]
|
||||
* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
|
||||
* :attr:`~django.views.generic.list.MultipleObjectMixin.context_object_name` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_context_object_name`]
|
||||
* :attr:`~django.views.generic.dates.DateMixin.date_field` [:meth:`~django.views.generic.dates.DateMixin.get_date_field`]
|
||||
* :attr:`~django.views.generic.base.View.http_method_names`
|
||||
|
@ -400,6 +410,7 @@ WeekArchiveView
|
|||
|
||||
* :attr:`~django.views.generic.list.MultipleObjectMixin.allow_empty` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_allow_empty`]
|
||||
* :attr:`~django.views.generic.dates.DateMixin.allow_future` [:meth:`~django.views.generic.dates.DateMixin.get_allow_future`]
|
||||
* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
|
||||
* :attr:`~django.views.generic.list.MultipleObjectMixin.context_object_name` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_context_object_name`]
|
||||
* :attr:`~django.views.generic.dates.DateMixin.date_field` [:meth:`~django.views.generic.dates.DateMixin.get_date_field`]
|
||||
* :attr:`~django.views.generic.base.View.http_method_names`
|
||||
|
@ -439,6 +450,7 @@ DayArchiveView
|
|||
|
||||
* :attr:`~django.views.generic.list.MultipleObjectMixin.allow_empty` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_allow_empty`]
|
||||
* :attr:`~django.views.generic.dates.DateMixin.allow_future` [:meth:`~django.views.generic.dates.DateMixin.get_allow_future`]
|
||||
* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
|
||||
* :attr:`~django.views.generic.list.MultipleObjectMixin.context_object_name` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_context_object_name`]
|
||||
* :attr:`~django.views.generic.dates.DateMixin.date_field` [:meth:`~django.views.generic.dates.DateMixin.get_date_field`]
|
||||
* :attr:`~django.views.generic.dates.DayMixin.day` [:meth:`~django.views.generic.dates.DayMixin.get_day`]
|
||||
|
@ -484,6 +496,7 @@ TodayArchiveView
|
|||
|
||||
* :attr:`~django.views.generic.list.MultipleObjectMixin.allow_empty` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_allow_empty`]
|
||||
* :attr:`~django.views.generic.dates.DateMixin.allow_future` [:meth:`~django.views.generic.dates.DateMixin.get_allow_future`]
|
||||
* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
|
||||
* :attr:`~django.views.generic.list.MultipleObjectMixin.context_object_name` [:meth:`~django.views.generic.list.MultipleObjectMixin.get_context_object_name`]
|
||||
* :attr:`~django.views.generic.dates.DateMixin.date_field` [:meth:`~django.views.generic.dates.DateMixin.get_date_field`]
|
||||
* :attr:`~django.views.generic.dates.DayMixin.day` [:meth:`~django.views.generic.dates.DayMixin.get_day`]
|
||||
|
@ -528,6 +541,7 @@ DateDetailView
|
|||
**Attributes** (with optional accessor):
|
||||
|
||||
* :attr:`~django.views.generic.dates.DateMixin.allow_future` [:meth:`~django.views.generic.dates.DateMixin.get_allow_future`]
|
||||
* :attr:`~django.views.generic.base.TemplateResponseMixin.content_type`
|
||||
* :attr:`~django.views.generic.detail.SingleObjectMixin.context_object_name` [:meth:`~django.views.generic.detail.SingleObjectMixin.get_context_object_name`]
|
||||
* :attr:`~django.views.generic.dates.DateMixin.date_field` [:meth:`~django.views.generic.dates.DateMixin.get_date_field`]
|
||||
* :attr:`~django.views.generic.dates.DayMixin.day` [:meth:`~django.views.generic.dates.DayMixin.get_day`]
|
||||
|
|
|
@ -64,6 +64,15 @@ TemplateResponseMixin
|
|||
instantiation, create a ``TemplateResponse`` subclass and assign it to
|
||||
``response_class``.
|
||||
|
||||
.. attribute:: content_type
|
||||
|
||||
.. versionadded:: 1.6
|
||||
The ``content_type`` attribute was added.
|
||||
|
||||
The content type to use for the response. ``content_type`` is passed
|
||||
as a keyword argument to ``response_class``. Default is ``None`` --
|
||||
meaning that Django uses :setting:`DEFAULT_CONTENT_TYPE`.
|
||||
|
||||
**Methods**
|
||||
|
||||
.. method:: render_to_response(context, **response_kwargs)
|
||||
|
|
|
@ -312,6 +312,11 @@ class TemplateViewTest(TestCase):
|
|||
|
||||
self.assertNotEqual(response.content, response2.content)
|
||||
|
||||
def test_content_type(self):
|
||||
response = self.client.get('/template/content_type/')
|
||||
self.assertEqual(response['Content-Type'], 'text/plain')
|
||||
|
||||
|
||||
class RedirectViewTest(unittest.TestCase):
|
||||
rf = RequestFactory()
|
||||
|
||||
|
|
|
@ -20,6 +20,8 @@ urlpatterns = patterns('',
|
|||
TemplateView.as_view(template_name='generic_views/about.html')),
|
||||
(r'^template/custom/(?P<foo>\w+)/$',
|
||||
views.CustomTemplateView.as_view(template_name='generic_views/about.html')),
|
||||
(r'^template/content_type/$',
|
||||
TemplateView.as_view(template_name='generic_views/robots.txt', content_type='text/plain')),
|
||||
|
||||
(r'^template/cached/(?P<foo>\w+)/$',
|
||||
cache_page(2.0)(TemplateView.as_view(template_name='generic_views/about.html'))),
|
||||
|
|
Loading…
Reference in New Issue