diff --git a/django/template/context.py b/django/template/context.py index 0c28b479cd..90825fcdb5 100644 --- a/django/template/context.py +++ b/django/template/context.py @@ -37,7 +37,9 @@ class BaseContext: self.dicts.append(value) def __copy__(self): - duplicate = copy(super()) + duplicate = BaseContext() + duplicate.__class__ = self.__class__ + duplicate.__dict__ = copy(self.__dict__) duplicate.dicts = self.dicts[:] return duplicate diff --git a/tests/template_tests/test_context.py b/tests/template_tests/test_context.py index 6d8ee7a6e6..f71cf1ff25 100644 --- a/tests/template_tests/test_context.py +++ b/tests/template_tests/test_context.py @@ -1,3 +1,4 @@ +from copy import copy from unittest import mock from django.http import HttpRequest @@ -314,3 +315,10 @@ class RequestContextTests(SimpleTestCase): with self.assertRaisesMessage(TypeError, msg): with request_context.bind_template(Template("")): pass + + def test_context_copyable(self): + request_context = RequestContext(HttpRequest()) + request_context_copy = copy(request_context) + self.assertIsInstance(request_context_copy, RequestContext) + self.assertEqual(request_context_copy.dicts, request_context.dicts) + self.assertIsNot(request_context_copy.dicts, request_context.dicts)