Code

Ticket #1504: 1504.diff

File 1504.diff, 2.4 KB (added by minarets, 7 years ago)

diff for code and docs

Line 
1Index: django/shortcuts/__init__.py
2===================================================================
3--- django/shortcuts/__init__.py        (revision 6188)
4+++ django/shortcuts/__init__.py        (working copy)
5@@ -14,7 +14,12 @@
6     Returns a HttpResponse whose content is filled with the result of calling
7     django.template.loader.render_to_string() with the passed arguments.
8     """
9-    return HttpResponse(loader.render_to_string(*args, **kwargs))
10+    if "mimetype" in kwargs:
11+        mimetype = kwargs["mimetype"]
12+        del kwargs["mimetype"]
13+        return HttpResponse(loader.render_to_string(*args, **kwargs), mimetype=mimetype)
14+    else:
15+        return HttpResponse(loader.render_to_string(*args, **kwargs))
16 load_and_render = render_to_response # For backwards compatibility.
17 
18 def _get_queryset(klass):
19Index: docs/templates_python.txt
20===================================================================
21--- docs/templates_python.txt   (revision 6188)
22+++ docs/templates_python.txt   (working copy)
23@@ -555,6 +555,41 @@
24 Django uses the template loaders in order according to the ``TEMPLATE_LOADERS``
25 setting. It uses each loader until a loader finds a match.
26 
27+The ``render_to_response()`` shortcut
28+=====================================
29+
30+Django provides a shortcut, ``django.shortcuts.render_to_response``, for loading a template, filling
31+its context, and returning `an HttpResponse object`_.
32+
33+**Required arguments:**
34+
35+    ``template``
36+        The full name of a template to use.
37+
38+**Optional arguments:**
39+
40+    ``context``
41+        A dictionary of values to add to the template
42+        context. By default, this is an empty dictionary. If a value in the
43+        dictionary is callable, the view will call it just before rendering
44+        the template.
45+
46+    ``mimetype``
47+        The MIME type to use for the resulting document. Defaults
48+        to the value of the ``DEFAULT_CONTENT_TYPE`` setting.
49+
50+**Example:**
51+
52+The following example renders the template ``myapp/index.html`` with the mimetype ``application/xhtml+xml``::
53+
54+    from django.shortcuts import render_to_response
55+   
56+    my_view(request):
57+        # View code here
58+        return render_to_response("myapp/index.html", {"foo": "bar"}, mimetype="application/xhtml+xml")
59+   
60+.. _an HttpResponse object: ../request_response/#httpresponse-objects
61+
62 Extending the template system
63 =============================
64