diff --git a/django/core/handlers/wsgi.py b/django/core/handlers/wsgi.py index 88be527d65..5032eed072 100644 --- a/django/core/handlers/wsgi.py +++ b/django/core/handlers/wsgi.py @@ -176,7 +176,7 @@ class WSGIHandler(base.BaseHandler): raise set_script_prefix(get_script_name(environ)) - signals.request_started.send(sender=self.__class__) + signals.request_started.send(sender=self.__class__, environ=environ) try: request = self.request_class(environ) except UnicodeDecodeError: diff --git a/django/core/signals.py b/django/core/signals.py index a14af009ed..c530a51f08 100644 --- a/django/core/signals.py +++ b/django/core/signals.py @@ -1,5 +1,5 @@ from django.dispatch import Signal -request_started = Signal() +request_started = Signal(providing_args=["environ"]) request_finished = Signal() got_request_exception = Signal(providing_args=["request"]) diff --git a/django/test/client.py b/django/test/client.py index 1952af27eb..f2bbbfe77d 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -98,7 +98,7 @@ class ClientHandler(BaseHandler): self.load_middleware() request_started.disconnect(close_old_connections) - request_started.send(sender=self.__class__) + request_started.send(sender=self.__class__, environ=environ) request_started.connect(close_old_connections) request = WSGIRequest(environ) # sneaky little hack so that we can easily get round diff --git a/docs/ref/signals.txt b/docs/ref/signals.txt index f2f1709cea..823545dd07 100644 --- a/docs/ref/signals.txt +++ b/docs/ref/signals.txt @@ -589,6 +589,12 @@ Arguments sent with this signal: ``sender`` The handler class -- e.g. ``django.core.handlers.wsgi.WsgiHandler`` -- that handled the request. +``environ`` + The ``environ`` dictionary provided to the request. + +.. versionchanged:: 1.8 + + The ``environ`` argument was added. request_finished ---------------- diff --git a/docs/releases/1.8.txt b/docs/releases/1.8.txt index 7d9a71c6e5..2fac733648 100644 --- a/docs/releases/1.8.txt +++ b/docs/releases/1.8.txt @@ -256,6 +256,10 @@ Signals :meth:`Signal.send_robust() ` now have their traceback attached as a ``__traceback__`` attribute. +* The ``environ`` argument, which contains the WSGI environment structure from + the request, was added to the :data:`~django.core.signals.request_started` + signal. + Templates ^^^^^^^^^ diff --git a/tests/handlers/tests.py b/tests/handlers/tests.py index 012b2ffa53..ee74affcc6 100644 --- a/tests/handlers/tests.py +++ b/tests/handlers/tests.py @@ -114,6 +114,7 @@ class SignalsTests(TestCase): def setUp(self): self.signals = [] + self.signaled_environ = None request_started.connect(self.register_started) request_finished.connect(self.register_finished) @@ -123,6 +124,7 @@ class SignalsTests(TestCase): def register_started(self, **kwargs): self.signals.append('started') + self.signaled_environ = kwargs.get('environ') def register_finished(self, **kwargs): self.signals.append('finished') @@ -131,6 +133,7 @@ class SignalsTests(TestCase): response = self.client.get('/regular/') self.assertEqual(self.signals, ['started', 'finished']) self.assertEqual(response.content, b"regular content") + self.assertEqual(self.signaled_environ, response.wsgi_request.environ) def test_request_signals_streaming_response(self): response = self.client.get('/streaming/')