From 18098d261fe98e0a3b7acbb47152031ff905f6a1 Mon Sep 17 00:00:00 2001 From: Matthew Power Date: Wed, 26 Sep 2018 20:35:24 +0100 Subject: [PATCH] Fixed #29673 -- Reset the URLconf at the end of each request. Co-authored-by: Ross Thorne --- django/core/handlers/base.py | 9 +++++++++ tests/urlpatterns_reverse/tests.py | 9 ++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/django/core/handlers/base.py b/django/core/handlers/base.py index edc166fab5..8eddad0e62 100644 --- a/django/core/handlers/base.py +++ b/django/core/handlers/base.py @@ -3,6 +3,7 @@ import types from django.conf import settings from django.core.exceptions import ImproperlyConfigured, MiddlewareNotUsed +from django.core.signals import request_finished from django.db import connections, transaction from django.urls import get_resolver, set_urlconf from django.utils.log import log_response @@ -167,3 +168,11 @@ class BaseHandler: if response: return response raise + + +def reset_urlconf(sender, **kwargs): + """Reset the URLconf after each request is finished.""" + set_urlconf(None) + + +request_finished.connect(reset_urlconf) diff --git a/tests/urlpatterns_reverse/tests.py b/tests/urlpatterns_reverse/tests.py index 037182f756..65024aeb0f 100644 --- a/tests/urlpatterns_reverse/tests.py +++ b/tests/urlpatterns_reverse/tests.py @@ -18,7 +18,7 @@ from django.test import SimpleTestCase, TestCase, override_settings from django.test.utils import override_script_prefix from django.urls import ( NoReverseMatch, Resolver404, ResolverMatch, URLPattern, URLResolver, - get_callable, get_resolver, resolve, reverse, reverse_lazy, + get_callable, get_resolver, get_urlconf, resolve, reverse, reverse_lazy, ) from django.urls.resolvers import RegexPattern @@ -1034,6 +1034,13 @@ class RequestURLconfTests(SimpleTestCase): self.client.get('/second_test/') b''.join(self.client.get('/second_test/')) + def test_urlconf_is_reset_after_request(self): + """The URLconf is reset after each request.""" + self.assertIsNone(get_urlconf()) + with override_settings(MIDDLEWARE=['%s.ChangeURLconfMiddleware' % middleware.__name__]): + self.client.get(reverse('inner')) + self.assertIsNone(get_urlconf()) + class ErrorHandlerResolutionTests(SimpleTestCase): """Tests for handler400, handler404 and handler500"""