Author: russellm Date: 2010-12-21 22:54:10 -0600 (Tue, 21 Dec 2010) New Revision: 15020
Modified: django/trunk/django/shortcuts/__init__.py django/trunk/docs/topics/http/shortcuts.txt django/trunk/tests/regressiontests/views/tests/shortcuts.py django/trunk/tests/regressiontests/views/urls.py django/trunk/tests/regressiontests/views/views.py Log: Fixed #14936 -- Tweaked the new render shortcut to reflect non-legacy arguments. Thanks to adamv for the report. Modified: django/trunk/django/shortcuts/__init__.py =================================================================== --- django/trunk/django/shortcuts/__init__.py 2010-12-22 04:12:32 UTC (rev 15019) +++ django/trunk/django/shortcuts/__init__.py 2010-12-22 04:54:10 UTC (rev 15020) @@ -25,7 +25,10 @@ django.template.loader.render_to_string() with the passed arguments. Uses a RequestContext by default. """ - httpresponse_kwargs = {'mimetype': kwargs.pop('mimetype', None)} + httpresponse_kwargs = { + 'content_type': kwargs.pop('content_type', None), + 'status': kwargs.pop('status', None), + } kwargs['context_instance'] = kwargs.get('context_instance', RequestContext(request)) return HttpResponse(loader.render_to_string(*args, **kwargs), **httpresponse_kwargs) Modified: django/trunk/docs/topics/http/shortcuts.txt =================================================================== --- django/trunk/docs/topics/http/shortcuts.txt 2010-12-22 04:12:32 UTC (rev 15019) +++ django/trunk/docs/topics/http/shortcuts.txt 2010-12-22 04:54:10 UTC (rev 15020) @@ -15,7 +15,7 @@ ``render`` ========== -.. function:: render(request, template[, dictionary][, context_instance][, mimetype]) +.. function:: render(request, template[, dictionary][, context_instance][, content_type][, status]) .. versionadded:: 1.3 @@ -48,22 +48,25 @@ will be rendered with a ``RequestContext`` instance (filled with values from ``request`` and ```dictionary``). -``mimetype`` +``content_type`` The MIME type to use for the resulting document. Defaults to the value of the :setting:`DEFAULT_CONTENT_TYPE` setting. +``status`` + The status code for the response. Defaults to ``200``. + Example ------- The following example renders the template ``myapp/index.html`` with the MIME type ``application/xhtml+xml``:: - from django.shortcuts import render_to_response + from django.shortcuts import render def my_view(request): # View code here... - return render_to_response('myapp/index.html', {"foo": "bar"}, - mimetype="application/xhtml+xml") + return render(request, 'myapp/index.html', {"foo": "bar"}, + content_type="application/xhtml+xml") This example is equivalent to:: @@ -75,7 +78,7 @@ t = loader.get_template('myapp/template.html') c = RequestContext(request, {'foo': 'bar'}) return HttpResponse(t.render(c), - mimetype="application/xhtml+xml") + content_type="application/xhtml+xml") ``render_to_response`` Modified: django/trunk/tests/regressiontests/views/tests/shortcuts.py =================================================================== --- django/trunk/tests/regressiontests/views/tests/shortcuts.py 2010-12-22 04:12:32 UTC (rev 15019) +++ django/trunk/tests/regressiontests/views/tests/shortcuts.py 2010-12-22 04:54:10 UTC (rev 15020) @@ -45,9 +45,14 @@ self.assertEquals(response.content, 'FOO.BAR..\n') self.assertEquals(response['Content-Type'], 'text/html; charset=utf-8') - def test_render_with_mimetype(self): - response = self.client.get('/views/shortcuts/render/mimetype/') + def test_render_with_content_type(self): + response = self.client.get('/views/shortcuts/render/content_type/') self.assertEquals(response.status_code, 200) self.assertEquals(response.content, 'FOO.BAR../path/to/static/media\n') self.assertEquals(response['Content-Type'], 'application/x-rendertest') + def test_render_with_status(self): + response = self.client.get('/views/shortcuts/render/status/') + self.assertEquals(response.status_code, 403) + self.assertEquals(response.content, 'FOO.BAR../path/to/static/media\n') + Modified: django/trunk/tests/regressiontests/views/urls.py =================================================================== --- django/trunk/tests/regressiontests/views/urls.py 2010-12-22 04:12:32 UTC (rev 15019) +++ django/trunk/tests/regressiontests/views/urls.py 2010-12-22 04:54:10 UTC (rev 15020) @@ -149,7 +149,8 @@ (r'^shortcuts/render_to_response/mimetype/$', 'render_to_response_view_with_mimetype'), (r'^shortcuts/render/$', 'render_view'), (r'^shortcuts/render/base_context/$', 'render_view_with_base_context'), - (r'^shortcuts/render/mimetype/$', 'render_view_with_mimetype'), + (r'^shortcuts/render/content_type/$', 'render_view_with_content_type'), + (r'^shortcuts/render/status/$', 'render_view_with_status'), ) Modified: django/trunk/tests/regressiontests/views/views.py =================================================================== --- django/trunk/tests/regressiontests/views/views.py 2010-12-22 04:12:32 UTC (rev 15019) +++ django/trunk/tests/regressiontests/views/views.py 2010-12-22 04:54:10 UTC (rev 15020) @@ -90,8 +90,14 @@ 'bar': 'BAR', }, context_instance=Context()) -def render_view_with_mimetype(request): +def render_view_with_content_type(request): return render(request, 'debug/render_test.html', { 'foo': 'FOO', 'bar': 'BAR', - }, mimetype='application/x-rendertest') + }, content_type='application/x-rendertest') + +def render_view_with_status(request): + return render(request, 'debug/render_test.html', { + 'foo': 'FOO', + 'bar': 'BAR', + }, status=403) -- You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-upda...@googlegroups.com. To unsubscribe from this group, send email to django-updates+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-updates?hl=en.