mirror of
https://github.com/django/django.git
synced 2025-10-30 17:16:10 +00:00
Avoided rewrapping Contexts in render_to_response.
This change preserves backwards-compatibility for a very common misuse
of render_to_response which even occurred in the official documentation.
It fixes that misuse wherever it happened in the code base and docs.
Context.__init__ is documented as accepting a dict and nothing else.
Since Context is dict-like, Context(Context({})) could work to some
extent. However, things get complicated with RequestContext and that
gets in the way of refactoring the template engine. This is the real
rationale for this change.
This commit is contained in:
@@ -65,7 +65,12 @@ def render_to_string(template_name, dictionary=None, context_instance=None,
|
||||
else:
|
||||
t = get_template(template_name, dirs)
|
||||
if not context_instance:
|
||||
return t.render(Context(dictionary))
|
||||
# Django < 1.8 accepted a Context in `dictionary` even though that's
|
||||
# unintended. Preserve this ability but don't rewrap `dictionary`.
|
||||
if isinstance(dictionary, Context):
|
||||
return t.render(dictionary)
|
||||
else:
|
||||
return t.render(Context(dictionary))
|
||||
if not dictionary:
|
||||
return t.render(context_instance)
|
||||
# Add the dictionary to the context stack, ensuring it gets removed again
|
||||
|
||||
Reference in New Issue
Block a user