Skip to content

Commit

Permalink
Fixed #3526 -- Added content_type as an alias for mimetype to the Htt…
Browse files Browse the repository at this point in the history
…pResponse constructor. It's a slightly more accurate name. Based on a patch from Simon Willison. Fully backwards compatible.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5844 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
malcolmt committed Aug 11, 2007
1 parent 6fd2c25 commit 0afbca9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
12 changes: 8 additions & 4 deletions django/http/__init__.py
Expand Up @@ -212,18 +212,22 @@ class HttpResponse(object):

status_code = 200

def __init__(self, content='', mimetype=None, status=None):
def __init__(self, content='', mimetype=None, status=None,
content_type=None):
from django.conf import settings
self._charset = settings.DEFAULT_CHARSET
if not mimetype:
mimetype = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE, settings.DEFAULT_CHARSET)
if mimetype:
content_type = mimetype # For backwards compatibility
if not content_type:
content_type = "%s; charset=%s" % (settings.DEFAULT_CONTENT_TYPE,
settings.DEFAULT_CHARSET)
if not isinstance(content, basestring) and hasattr(content, '__iter__'):
self._container = content
self._is_string = False
else:
self._container = [content]
self._is_string = True
self.headers = {'Content-Type': mimetype}
self.headers = {'Content-Type': content_type}
self.cookies = SimpleCookie()
if status:
self.status_code = status
Expand Down
14 changes: 13 additions & 1 deletion docs/request_response.txt
Expand Up @@ -342,14 +342,24 @@ hard-coded strings. If you use this technique, follow these guidelines:
Methods
-------

``__init__(content='', mimetype=DEFAULT_CONTENT_TYPE)``
``__init__(content='', mimetype=None, status=200, content_type=DEFAULT_CONTENT_TYPE)``
Instantiates an ``HttpResponse`` object with the given page content (a
string) and MIME type. The ``DEFAULT_CONTENT_TYPE`` is ``'text/html'``.

``content`` can be an iterator or a string. If it's an iterator, it should
return strings, and those strings will be joined together to form the
content of the response.

``status`` is the `HTTP Status code`_ for the response.

**(New in Django development version)** ``content_type`` is an alias for
``mimetype``. Historically, the parameter was only called ``mimetype``,
but since this is actually the value included in the HTTP ``Content-Type``
header, it can also include the character set encoding, which makes it
more than just a MIME type specification. If ``mimetype`` is specifiedi
(not None), that value is used. Otherwise, ``content_type`` is used. If
neither is given, the ``DEFAULT_CONTENT_TYPE`` setting is used.

``__setitem__(header, value)``
Sets the given header name to the given value. Both ``header`` and
``value`` should be strings.
Expand Down Expand Up @@ -396,6 +406,8 @@ Methods
``write(content)``, ``flush()`` and ``tell()``
These methods make an ``HttpResponse`` instance a file-like object.

.. _HTTP Status code: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10

HttpResponse subclasses
-----------------------

Expand Down

0 comments on commit 0afbca9

Please sign in to comment.