diff options
| author | David Sanders <shang.xiao.sanders@gmail.com> | 2023-03-29 17:54:04 +1100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-03-29 08:54:04 +0200 |
| commit | 5dba5fda551afe169dce7620b08a99c3455ebb00 (patch) | |
| tree | 10b1156a8e519ed3d10a4108e5f6ca11aa10df86 | |
| parent | 996c802229b93fe83c39842e56c6b8668464deaf (diff) | |
Fixed #34427 -- Improved error message when context processor does not return a dict.
| -rw-r--r-- | django/template/context.py | 10 | ||||
| -rw-r--r-- | tests/template_tests/test_context.py | 29 |
2 files changed, 37 insertions, 2 deletions
diff --git a/django/template/context.py b/django/template/context.py index ccf0b430dc..080a2dd9c0 100644 --- a/django/template/context.py +++ b/django/template/context.py @@ -251,7 +251,15 @@ class RequestContext(Context): processors = template.engine.template_context_processors + self._processors updates = {} for processor in processors: - updates.update(processor(self.request)) + context = processor(self.request) + try: + updates.update(context) + except TypeError as e: + raise TypeError( + f"Context processor {processor.__qualname__} didn't return a " + "dictionary." + ) from e + self.dicts[self._processors_index] = updates try: diff --git a/tests/template_tests/test_context.py b/tests/template_tests/test_context.py index 4feb9e57d6..7420bb4c36 100644 --- a/tests/template_tests/test_context.py +++ b/tests/template_tests/test_context.py @@ -10,7 +10,7 @@ from django.template import ( VariableDoesNotExist, ) from django.template.context import RenderContext -from django.test import RequestFactory, SimpleTestCase +from django.test import RequestFactory, SimpleTestCase, override_settings class ContextTests(SimpleTestCase): @@ -222,6 +222,10 @@ class ContextTests(SimpleTestCase): self.assertEqual(c.dicts[-1]["a"], 2) +def context_process_returning_none(request): + return None + + class RequestContextTests(SimpleTestCase): request_factory = RequestFactory() @@ -276,3 +280,26 @@ class RequestContextTests(SimpleTestCase): context = RequestContext(request, {}) context["foo"] = "foo" self.assertEqual(template.render(context), "foo") + + @override_settings( + TEMPLATES=[ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.request", + "template_tests.test_context.context_process_returning_none", + ], + }, + } + ], + ) + def test_template_context_processor_returning_none(self): + request_context = RequestContext(HttpRequest()) + msg = ( + "Context processor context_process_returning_none didn't return a " + "dictionary." + ) + with self.assertRaisesMessage(TypeError, msg): + with request_context.bind_template(Template("")): + pass |
