Skip to content
/ django Public

Commit 0fef92f

Browse files
committed
Fixed #14936 -- Tweaked the new render shortcut to reflect non-legacy arguments. Thanks to adamv for the report.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@15020 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent 6e75ee2 commit 0fef92f

File tree

5 files changed

+30
-12
lines changed

5 files changed

+30
-12
lines changed

django/shortcuts/__init__.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ def render(request, *args, **kwargs):
2525
django.template.loader.render_to_string() with the passed arguments.
2626
Uses a RequestContext by default.
2727
"""
28-
httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)}
28+
httpresponse_kwargs = {
29+
'content_type': kwargs.pop('content_type', None),
30+
'status': kwargs.pop('status', None),
31+
}
2932
kwargs['context_instance'] = kwargs.get('context_instance', RequestContext(request))
3033
return HttpResponse(loader.render_to_string(*args, **kwargs),
3134
**httpresponse_kwargs)

docs/topics/http/shortcuts.txt

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ introduce controlled coupling for convenience's sake.
1515
``render``
1616
==========
1717

18-
.. function:: render(request, template[, dictionary][, context_instance][, mimetype])
18+
.. function:: render(request, template[, dictionary][, context_instance][, content_type][, status])
1919

2020
.. versionadded:: 1.3
2121

@@ -48,22 +48,25 @@ Optional arguments
4848
will be rendered with a ``RequestContext`` instance (filled with values from
4949
``request`` and ```dictionary``).
5050

51-
``mimetype``
51+
``content_type``
5252
The MIME type to use for the resulting document. Defaults to the value of
5353
the :setting:`DEFAULT_CONTENT_TYPE` setting.
5454

55+
``status``
56+
The status code for the response. Defaults to ``200``.
57+
5558
Example
5659
-------
5760

5861
The following example renders the template ``myapp/index.html`` with the
5962
MIME type ``application/xhtml+xml``::
6063

61-
from django.shortcuts import render_to_response
64+
from django.shortcuts import render
6265

6366
def my_view(request):
6467
# View code here...
65-
return render_to_response('myapp/index.html', {"foo": "bar"},
66-
mimetype="application/xhtml+xml")
68+
return render(request, 'myapp/index.html', {"foo": "bar"},
69+
content_type="application/xhtml+xml")
6770

6871
This example is equivalent to::
6972

@@ -75,7 +78,7 @@ This example is equivalent to::
7578
t = loader.get_template('myapp/template.html')
7679
c = RequestContext(request, {'foo': 'bar'})
7780
return HttpResponse(t.render(c),
78-
mimetype="application/xhtml+xml")
81+
content_type="application/xhtml+xml")
7982

8083

8184
``render_to_response``

tests/regressiontests/views/tests/shortcuts.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,14 @@ def test_render_with_base_context(self):
4545
self.assertEquals(response.content, 'FOO.BAR..\n')
4646
self.assertEquals(response['Content-Type'], 'text/html; charset=utf-8')
4747

48-
def test_render_with_mimetype(self):
49-
response = self.client.get('/views/shortcuts/render/mimetype/')
48+
def test_render_with_content_type(self):
49+
response = self.client.get('/views/shortcuts/render/content_type/')
5050
self.assertEquals(response.status_code, 200)
5151
self.assertEquals(response.content, 'FOO.BAR../path/to/static/media\n')
5252
self.assertEquals(response['Content-Type'], 'application/x-rendertest')
5353

54+
def test_render_with_status(self):
55+
response = self.client.get('/views/shortcuts/render/status/')
56+
self.assertEquals(response.status_code, 403)
57+
self.assertEquals(response.content, 'FOO.BAR../path/to/static/media\n')
58+

tests/regressiontests/views/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@
149149
(r'^shortcuts/render_to_response/mimetype/$', 'render_to_response_view_with_mimetype'),
150150
(r'^shortcuts/render/$', 'render_view'),
151151
(r'^shortcuts/render/base_context/$', 'render_view_with_base_context'),
152-
(r'^shortcuts/render/mimetype/$', 'render_view_with_mimetype'),
152+
(r'^shortcuts/render/content_type/$', 'render_view_with_content_type'),
153+
(r'^shortcuts/render/status/$', 'render_view_with_status'),
153154

154155
)
155156

tests/regressiontests/views/views.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,14 @@ def render_view_with_base_context(request):
9090
'bar': 'BAR',
9191
}, context_instance=Context())
9292

93-
def render_view_with_mimetype(request):
93+
def render_view_with_content_type(request):
9494
return render(request, 'debug/render_test.html', {
9595
'foo': 'FOO',
9696
'bar': 'BAR',
97-
}, mimetype='application/x-rendertest')
97+
}, content_type='application/x-rendertest')
98+
99+
def render_view_with_status(request):
100+
return render(request, 'debug/render_test.html', {
101+
'foo': 'FOO',
102+
'bar': 'BAR',
103+
}, status=403)

0 commit comments

Comments
 (0)