[1.5.x] Fixed #19692 -- Completed deprecation of mimetype in favor of content_type.
Thanks Tim for the report and initial patch.
Backport of 89cb771
from master.
This commit is contained in:
parent
256352a790
commit
11ec0253ab
|
@ -1,3 +1,5 @@
|
||||||
|
import warnings
|
||||||
|
|
||||||
from django.contrib.sites.models import get_current_site
|
from django.contrib.sites.models import get_current_site
|
||||||
from django.core import urlresolvers
|
from django.core import urlresolvers
|
||||||
from django.core.paginator import EmptyPage, PageNotAnInteger
|
from django.core.paginator import EmptyPage, PageNotAnInteger
|
||||||
|
@ -6,8 +8,15 @@ from django.template.response import TemplateResponse
|
||||||
from django.utils import six
|
from django.utils import six
|
||||||
|
|
||||||
def index(request, sitemaps,
|
def index(request, sitemaps,
|
||||||
template_name='sitemap_index.xml', mimetype='application/xml',
|
template_name='sitemap_index.xml', content_type='application/xml',
|
||||||
sitemap_url_name='django.contrib.sitemaps.views.sitemap'):
|
sitemap_url_name='django.contrib.sitemaps.views.sitemap',
|
||||||
|
mimetype=None):
|
||||||
|
|
||||||
|
if mimetype:
|
||||||
|
warnings.warn("The mimetype keyword argument is deprecated, use "
|
||||||
|
"content_type instead", PendingDeprecationWarning, stacklevel=2)
|
||||||
|
content_type = mimetype
|
||||||
|
|
||||||
req_protocol = 'https' if request.is_secure() else 'http'
|
req_protocol = 'https' if request.is_secure() else 'http'
|
||||||
req_site = get_current_site(request)
|
req_site = get_current_site(request)
|
||||||
|
|
||||||
|
@ -24,10 +33,17 @@ def index(request, sitemaps,
|
||||||
sites.append('%s?p=%s' % (absolute_url, page))
|
sites.append('%s?p=%s' % (absolute_url, page))
|
||||||
|
|
||||||
return TemplateResponse(request, template_name, {'sitemaps': sites},
|
return TemplateResponse(request, template_name, {'sitemaps': sites},
|
||||||
content_type=mimetype)
|
content_type=content_type)
|
||||||
|
|
||||||
def sitemap(request, sitemaps, section=None,
|
def sitemap(request, sitemaps, section=None,
|
||||||
template_name='sitemap.xml', mimetype='application/xml'):
|
template_name='sitemap.xml', content_type='application/xml',
|
||||||
|
mimetype=None):
|
||||||
|
|
||||||
|
if mimetype:
|
||||||
|
warnings.warn("The mimetype keyword argument is deprecated, use "
|
||||||
|
"content_type instead", PendingDeprecationWarning, stacklevel=2)
|
||||||
|
content_type = mimetype
|
||||||
|
|
||||||
req_protocol = 'https' if request.is_secure() else 'http'
|
req_protocol = 'https' if request.is_secure() else 'http'
|
||||||
req_site = get_current_site(request)
|
req_site = get_current_site(request)
|
||||||
|
|
||||||
|
@ -51,4 +67,4 @@ def sitemap(request, sitemaps, section=None,
|
||||||
except PageNotAnInteger:
|
except PageNotAnInteger:
|
||||||
raise Http404("No page '%s'" % page)
|
raise Http404("No page '%s'" % page)
|
||||||
return TemplateResponse(request, template_name, {'urlset': urls},
|
return TemplateResponse(request, template_name, {'urlset': urls},
|
||||||
content_type=mimetype)
|
content_type=content_type)
|
||||||
|
|
|
@ -3,6 +3,7 @@ This module collects helper functions and classes that "span" multiple levels
|
||||||
of MVC. In other words, these functions/classes introduce controlled coupling
|
of MVC. In other words, these functions/classes introduce controlled coupling
|
||||||
for convenience's sake.
|
for convenience's sake.
|
||||||
"""
|
"""
|
||||||
|
import warnings
|
||||||
|
|
||||||
from django.template import loader, RequestContext
|
from django.template import loader, RequestContext
|
||||||
from django.http import HttpResponse, Http404
|
from django.http import HttpResponse, Http404
|
||||||
|
@ -17,7 +18,14 @@ def render_to_response(*args, **kwargs):
|
||||||
Returns a HttpResponse whose content is filled with the result of calling
|
Returns a HttpResponse whose content is filled with the result of calling
|
||||||
django.template.loader.render_to_string() with the passed arguments.
|
django.template.loader.render_to_string() with the passed arguments.
|
||||||
"""
|
"""
|
||||||
httpresponse_kwargs = {'content_type': kwargs.pop('mimetype', None)}
|
httpresponse_kwargs = {'content_type': kwargs.pop('content_type', None)}
|
||||||
|
|
||||||
|
mimetype = kwargs.pop('mimetype', None)
|
||||||
|
if mimetype:
|
||||||
|
warnings.warn("The mimetype keyword argument is deprecated, use "
|
||||||
|
"content_type instead", PendingDeprecationWarning, stacklevel=2)
|
||||||
|
httpresponse_kwargs['content_type'] = mimetype
|
||||||
|
|
||||||
return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
|
return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs)
|
||||||
|
|
||||||
def render(request, *args, **kwargs):
|
def render(request, *args, **kwargs):
|
||||||
|
|
|
@ -20,7 +20,7 @@ Here's an example::
|
||||||
|
|
||||||
def some_view(request):
|
def some_view(request):
|
||||||
# Create the HttpResponse object with the appropriate CSV header.
|
# Create the HttpResponse object with the appropriate CSV header.
|
||||||
response = HttpResponse(mimetype='text/csv')
|
response = HttpResponse(content_type='text/csv')
|
||||||
response['Content-Disposition'] = 'attachment; filename="somefilename.csv"'
|
response['Content-Disposition'] = 'attachment; filename="somefilename.csv"'
|
||||||
|
|
||||||
writer = csv.writer(response)
|
writer = csv.writer(response)
|
||||||
|
@ -92,7 +92,7 @@ Here's an example, which generates the same CSV file as above::
|
||||||
|
|
||||||
def some_view(request):
|
def some_view(request):
|
||||||
# Create the HttpResponse object with the appropriate CSV header.
|
# Create the HttpResponse object with the appropriate CSV header.
|
||||||
response = HttpResponse(mimetype='text/csv')
|
response = HttpResponse(content_type='text/csv')
|
||||||
response['Content-Disposition'] = 'attachment; filename="somefilename.csv"'
|
response['Content-Disposition'] = 'attachment; filename="somefilename.csv"'
|
||||||
|
|
||||||
# The data is hard-coded here, but you could load it from a database or
|
# The data is hard-coded here, but you could load it from a database or
|
||||||
|
@ -111,7 +111,7 @@ Here's an example, which generates the same CSV file as above::
|
||||||
|
|
||||||
The only difference between this example and the previous example is that this
|
The only difference between this example and the previous example is that this
|
||||||
one uses template loading instead of the CSV module. The rest of the code --
|
one uses template loading instead of the CSV module. The rest of the code --
|
||||||
such as the ``mimetype='text/csv'`` -- is the same.
|
such as the ``content_type='text/csv'`` -- is the same.
|
||||||
|
|
||||||
Then, create the template ``my_template_name.txt``, with this template code:
|
Then, create the template ``my_template_name.txt``, with this template code:
|
||||||
|
|
||||||
|
|
|
@ -51,7 +51,7 @@ Here's a "Hello World" example::
|
||||||
|
|
||||||
def some_view(request):
|
def some_view(request):
|
||||||
# Create the HttpResponse object with the appropriate PDF headers.
|
# Create the HttpResponse object with the appropriate PDF headers.
|
||||||
response = HttpResponse(mimetype='application/pdf')
|
response = HttpResponse(content_type='application/pdf')
|
||||||
response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
|
response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
|
||||||
|
|
||||||
# Create the PDF object, using the response object as its "file."
|
# Create the PDF object, using the response object as its "file."
|
||||||
|
@ -120,7 +120,7 @@ Here's the above "Hello World" example rewritten to use :mod:`io`::
|
||||||
|
|
||||||
def some_view(request):
|
def some_view(request):
|
||||||
# Create the HttpResponse object with the appropriate PDF headers.
|
# Create the HttpResponse object with the appropriate PDF headers.
|
||||||
response = HttpResponse(mimetype='application/pdf')
|
response = HttpResponse(content_type='application/pdf')
|
||||||
response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
|
response['Content-Disposition'] = 'attachment; filename="somefilename.pdf"'
|
||||||
|
|
||||||
buffer = BytesIO()
|
buffer = BytesIO()
|
||||||
|
|
|
@ -290,8 +290,14 @@ these changes.
|
||||||
specified as a plain string instead of a tuple will be removed and raise an
|
specified as a plain string instead of a tuple will be removed and raise an
|
||||||
exception.
|
exception.
|
||||||
|
|
||||||
* The ``mimetype`` argument to :class:`~django.http.HttpResponse` ``__init__``
|
* The ``mimetype`` argument to the ``__init__`` methods of
|
||||||
will be removed (``content_type`` should be used instead).
|
:class:`~django.http.HttpResponse`,
|
||||||
|
:class:`~django.template.response.SimpleTemplateResponse`, and
|
||||||
|
:class:`~django.template.response.TemplateResponse`, will be removed.
|
||||||
|
``content_type`` should be used instead. This also applies to the
|
||||||
|
:func:`~django.shortcuts.render_to_response` shortcut and
|
||||||
|
the sitemamp views, :func:`~django.contrib.sitemaps.views.index` and
|
||||||
|
:func:`~django.contrib.sitemaps.views.sitemap`.
|
||||||
|
|
||||||
* When :class:`~django.http.HttpResponse` is instantiated with an iterator,
|
* When :class:`~django.http.HttpResponse` is instantiated with an iterator,
|
||||||
or when :attr:`~django.http.HttpResponse.content` is set to an iterator,
|
or when :attr:`~django.http.HttpResponse.content` is set to an iterator,
|
||||||
|
|
|
@ -223,7 +223,7 @@ objects as JSON::
|
||||||
from django.core import serializers
|
from django.core import serializers
|
||||||
|
|
||||||
def export_as_json(modeladmin, request, queryset):
|
def export_as_json(modeladmin, request, queryset):
|
||||||
response = HttpResponse(mimetype="text/javascript")
|
response = HttpResponse(content_type="application/json")
|
||||||
serializers.serialize("json", queryset, stream=response)
|
serializers.serialize("json", queryset, stream=response)
|
||||||
return response
|
return response
|
||||||
|
|
||||||
|
@ -356,5 +356,3 @@ Conditionally enabling or disabling actions
|
||||||
if 'delete_selected' in actions:
|
if 'delete_selected' in actions:
|
||||||
del actions['delete_selected']
|
del actions['delete_selected']
|
||||||
return actions
|
return actions
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -56,11 +56,11 @@ Attributes
|
||||||
Methods
|
Methods
|
||||||
-------
|
-------
|
||||||
|
|
||||||
.. method:: SimpleTemplateResponse.__init__(template, context=None, mimetype=None, status=None, content_type=None)
|
.. method:: SimpleTemplateResponse.__init__(template, context=None, content_type=None, status=None)
|
||||||
|
|
||||||
Instantiates a
|
Instantiates a
|
||||||
:class:`~django.template.response.SimpleTemplateResponse` object
|
:class:`~django.template.response.SimpleTemplateResponse` object
|
||||||
with the given template, context, MIME type and HTTP status.
|
with the given template, context, content type, and HTTP status.
|
||||||
|
|
||||||
``template``
|
``template``
|
||||||
The full name of a template, or a sequence of template names.
|
The full name of a template, or a sequence of template names.
|
||||||
|
@ -75,12 +75,15 @@ Methods
|
||||||
The HTTP Status code for the response.
|
The HTTP Status code for the response.
|
||||||
|
|
||||||
``content_type``
|
``content_type``
|
||||||
An alias for ``mimetype``. Historically, this parameter was only called
|
|
||||||
``mimetype``, but since this is actually the value included in the HTTP
|
.. versionchanged:: 1.5
|
||||||
``Content-Type`` header, it can also include the character set encoding,
|
|
||||||
which makes it more than just a MIME type specification. If ``mimetype``
|
Historically, this parameter was only called ``mimetype`` (now
|
||||||
is specified (not ``None``), that value is used. Otherwise,
|
deprecated), but since this is actually the value included in the HTTP
|
||||||
``content_type`` is used. If neither is given,
|
``Content-Type`` header, it can also include the character set
|
||||||
|
encoding, which makes it more than just a MIME type specification. If
|
||||||
|
``mimetype`` is specified (not ``None``), that value is used.
|
||||||
|
Otherwise, ``content_type`` is used. If neither is given,
|
||||||
:setting:`DEFAULT_CONTENT_TYPE` is used.
|
:setting:`DEFAULT_CONTENT_TYPE` is used.
|
||||||
|
|
||||||
|
|
||||||
|
@ -144,7 +147,7 @@ TemplateResponse objects
|
||||||
Methods
|
Methods
|
||||||
-------
|
-------
|
||||||
|
|
||||||
.. method:: TemplateResponse.__init__(request, template, context=None, mimetype=None, status=None, content_type=None, current_app=None)
|
.. method:: TemplateResponse.__init__(request, template, context=None, content_type=None, status=None, current_app=None)
|
||||||
|
|
||||||
Instantiates an ``TemplateResponse`` object with the given
|
Instantiates an ``TemplateResponse`` object with the given
|
||||||
template, context, MIME type and HTTP status.
|
template, context, MIME type and HTTP status.
|
||||||
|
@ -165,12 +168,15 @@ Methods
|
||||||
The HTTP Status code for the response.
|
The HTTP Status code for the response.
|
||||||
|
|
||||||
``content_type``
|
``content_type``
|
||||||
An alias for ``mimetype``. Historically, this parameter was only called
|
|
||||||
``mimetype``, but since this is actually the value included in the HTTP
|
.. versionchanged:: 1.5
|
||||||
``Content-Type`` header, it can also include the character set encoding,
|
|
||||||
which makes it more than just a MIME type specification. If ``mimetype``
|
Historically, this parameter was only called ``mimetype`` (now
|
||||||
is specified (not ``None``), that value is used. Otherwise,
|
deprecated), but since this is actually the value included in the HTTP
|
||||||
``content_type`` is used. If neither is given,
|
``Content-Type`` header, it can also include the character set
|
||||||
|
encoding, which makes it more than just a MIME type specification. If
|
||||||
|
``mimetype`` is specified (not ``None``), that value is used.
|
||||||
|
Otherwise, ``content_type`` is used. If neither is given,
|
||||||
:setting:`DEFAULT_CONTENT_TYPE` is used.
|
:setting:`DEFAULT_CONTENT_TYPE` is used.
|
||||||
|
|
||||||
``current_app``
|
``current_app``
|
||||||
|
|
|
@ -50,6 +50,9 @@ Optional arguments
|
||||||
The MIME type to use for the resulting document. Defaults to the value of
|
The MIME type to use for the resulting document. Defaults to the value of
|
||||||
the :setting:`DEFAULT_CONTENT_TYPE` setting.
|
the :setting:`DEFAULT_CONTENT_TYPE` setting.
|
||||||
|
|
||||||
|
.. versionchanged:: 1.5
|
||||||
|
This parameter used to be called ``mimetype``.
|
||||||
|
|
||||||
``status``
|
``status``
|
||||||
The status code for the response. Defaults to ``200``.
|
The status code for the response. Defaults to ``200``.
|
||||||
|
|
||||||
|
@ -87,7 +90,7 @@ This example is equivalent to::
|
||||||
``render_to_response``
|
``render_to_response``
|
||||||
======================
|
======================
|
||||||
|
|
||||||
.. function:: render_to_response(template_name[, dictionary][, context_instance][, mimetype])
|
.. function:: render_to_response(template_name[, dictionary][, context_instance][, content_type])
|
||||||
|
|
||||||
Renders a given template with a given context dictionary and returns an
|
Renders a given template with a given context dictionary and returns an
|
||||||
:class:`~django.http.HttpResponse` object with that rendered text.
|
:class:`~django.http.HttpResponse` object with that rendered text.
|
||||||
|
@ -121,10 +124,14 @@ Optional arguments
|
||||||
my_data_dictionary,
|
my_data_dictionary,
|
||||||
context_instance=RequestContext(request))
|
context_instance=RequestContext(request))
|
||||||
|
|
||||||
``mimetype``
|
``content_type``
|
||||||
The MIME type to use for the resulting document. Defaults to the value of
|
The MIME type to use for the resulting document. Defaults to the value of
|
||||||
the :setting:`DEFAULT_CONTENT_TYPE` setting.
|
the :setting:`DEFAULT_CONTENT_TYPE` setting.
|
||||||
|
|
||||||
|
.. versionchanged:: 1.5
|
||||||
|
This parameter used to be called ``mimetype``.
|
||||||
|
|
||||||
|
|
||||||
Example
|
Example
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
@ -148,7 +155,7 @@ This example is equivalent to::
|
||||||
t = loader.get_template('myapp/template.html')
|
t = loader.get_template('myapp/template.html')
|
||||||
c = Context({'foo': 'bar'})
|
c = Context({'foo': 'bar'})
|
||||||
return HttpResponse(t.render(c),
|
return HttpResponse(t.render(c),
|
||||||
mimetype="application/xhtml+xml")
|
content_type="application/xhtml+xml")
|
||||||
|
|
||||||
``redirect``
|
``redirect``
|
||||||
============
|
============
|
||||||
|
|
|
@ -47,7 +47,7 @@ urlpatterns += patterns('',
|
||||||
urlpatterns += patterns('regressiontests.views.views',
|
urlpatterns += patterns('regressiontests.views.views',
|
||||||
(r'^shortcuts/render_to_response/$', 'render_to_response_view'),
|
(r'^shortcuts/render_to_response/$', 'render_to_response_view'),
|
||||||
(r'^shortcuts/render_to_response/request_context/$', 'render_to_response_view_with_request_context'),
|
(r'^shortcuts/render_to_response/request_context/$', 'render_to_response_view_with_request_context'),
|
||||||
(r'^shortcuts/render_to_response/mimetype/$', 'render_to_response_view_with_mimetype'),
|
(r'^shortcuts/render_to_response/content_type/$', 'render_to_response_view_with_content_type'),
|
||||||
(r'^shortcuts/render/$', 'render_view'),
|
(r'^shortcuts/render/$', 'render_view'),
|
||||||
(r'^shortcuts/render/base_context/$', 'render_view_with_base_context'),
|
(r'^shortcuts/render/base_context/$', 'render_view_with_base_context'),
|
||||||
(r'^shortcuts/render/content_type/$', 'render_view_with_content_type'),
|
(r'^shortcuts/render/content_type/$', 'render_view_with_content_type'),
|
||||||
|
|
|
@ -21,8 +21,8 @@ class ShortcutTests(TestCase):
|
||||||
self.assertEqual(response.content, b'FOO.BAR../path/to/static/media/\n')
|
self.assertEqual(response.content, b'FOO.BAR../path/to/static/media/\n')
|
||||||
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
|
self.assertEqual(response['Content-Type'], 'text/html; charset=utf-8')
|
||||||
|
|
||||||
def test_render_to_response_with_mimetype(self):
|
def test_render_to_response_with_content_type(self):
|
||||||
response = self.client.get('/shortcuts/render_to_response/mimetype/')
|
response = self.client.get('/shortcuts/render_to_response/content_type/')
|
||||||
self.assertEqual(response.status_code, 200)
|
self.assertEqual(response.status_code, 200)
|
||||||
self.assertEqual(response.content, b'FOO.BAR..\n')
|
self.assertEqual(response.content, b'FOO.BAR..\n')
|
||||||
self.assertEqual(response['Content-Type'], 'application/x-rendertest')
|
self.assertEqual(response['Content-Type'], 'application/x-rendertest')
|
||||||
|
|
|
@ -68,11 +68,11 @@ def render_to_response_view_with_request_context(request):
|
||||||
'bar': 'BAR',
|
'bar': 'BAR',
|
||||||
}, context_instance=RequestContext(request))
|
}, context_instance=RequestContext(request))
|
||||||
|
|
||||||
def render_to_response_view_with_mimetype(request):
|
def render_to_response_view_with_content_type(request):
|
||||||
return render_to_response('debug/render_test.html', {
|
return render_to_response('debug/render_test.html', {
|
||||||
'foo': 'FOO',
|
'foo': 'FOO',
|
||||||
'bar': 'BAR',
|
'bar': 'BAR',
|
||||||
}, mimetype='application/x-rendertest')
|
}, content_type='application/x-rendertest')
|
||||||
|
|
||||||
def render_view(request):
|
def render_view(request):
|
||||||
return render(request, 'debug/render_test.html', {
|
return render(request, 'debug/render_test.html', {
|
||||||
|
|
Loading…
Reference in New Issue