summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Beaven <smileychris@gmail.com>2011-02-20 05:56:45 +0000
committerChris Beaven <smileychris@gmail.com>2011-02-20 05:56:45 +0000
commit2377f109bca866821fe7556c3da19dbe0178e291 (patch)
treecf607384f367b0d7ea68ee6468a101e4c7014067
parent69a3bb5d473c864124e57541e9dde8496da3ac15 (diff)
[1.2.X] Ensure render_to_string leaves the context instance stack in the state it was originally passed in.
Backport of r15591 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15592 bcc190cf-cafb-0310-a4f2-bffc1f526a37
-rw-r--r--django/template/loader.py14
-rw-r--r--tests/regressiontests/templates/loaders.py25
-rw-r--r--tests/regressiontests/templates/templates/test_context.html1
3 files changed, 35 insertions, 5 deletions
diff --git a/django/template/loader.py b/django/template/loader.py
index b8077522fc..99044619b0 100644
--- a/django/template/loader.py
+++ b/django/template/loader.py
@@ -179,11 +179,15 @@ def render_to_string(template_name, dictionary=None, context_instance=None):
t = select_template(template_name)
else:
t = get_template(template_name)
- if context_instance:
- context_instance.update(dictionary)
- else:
- context_instance = Context(dictionary)
- return t.render(context_instance)
+ if not context_instance:
+ return t.render(Context(dictionary))
+ # Add the dictionary to the context stack, ensuring it gets removed again
+ # to keep the context_instance in the same state it started in.
+ context_instance.update(dictionary)
+ try:
+ return t.render(context_instance)
+ finally:
+ context_instance.pop()
def select_template(template_name_list):
"Given a list of template names, returns the first that can be loaded."
diff --git a/tests/regressiontests/templates/loaders.py b/tests/regressiontests/templates/loaders.py
index 47cd18ae21..948d3d7e55 100644
--- a/tests/regressiontests/templates/loaders.py
+++ b/tests/regressiontests/templates/loaders.py
@@ -146,5 +146,30 @@ class CachedLoader(unittest.TestCase):
# The two templates should not have the same content
self.assertNotEqual(t1.render(Context({})), t2.render(Context({})))
+class RenderToStringTest(unittest.TestCase):
+
+ def setUp(self):
+ self._old_TEMPLATE_DIRS = settings.TEMPLATE_DIRS
+ settings.TEMPLATE_DIRS = (
+ os.path.join(os.path.dirname(__file__), 'templates'),
+ )
+
+ def tearDown(self):
+ settings.TEMPLATE_DIRS = self._old_TEMPLATE_DIRS
+
+ def test_basic(self):
+ self.assertEqual(loader.render_to_string('test_context.html'), 'obj:')
+
+ def test_basic_context(self):
+ self.assertEqual(loader.render_to_string('test_context.html',
+ {'obj': 'test'}), 'obj:test')
+
+ def test_existing_context_kept_clean(self):
+ context = Context({'obj': 'before'})
+ output = loader.render_to_string('test_context.html', {'obj': 'after'},
+ context_instance=context)
+ self.assertEqual(output, 'obj:after')
+ self.assertEqual(context['obj'], 'before')
+
if __name__ == "__main__":
unittest.main()
diff --git a/tests/regressiontests/templates/templates/test_context.html b/tests/regressiontests/templates/templates/test_context.html
new file mode 100644
index 0000000000..a100f03de6
--- /dev/null
+++ b/tests/regressiontests/templates/templates/test_context.html
@@ -0,0 +1 @@
+obj:{{ obj }} \ No newline at end of file