Loading django/core/context_processors.py +6 −2 Original line number Diff line number Diff line Loading @@ -35,12 +35,16 @@ def csrf(request): def debug(request): "Returns context variables helpful for debugging." """ Returns context variables helpful for debugging. """ context_extras = {} if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS: context_extras['debug'] = True from django.db import connection context_extras['sql_queries'] = connection.queries # Return a lazy reference that computes connection.queries on access, # to ensure it contains queries triggered after this function runs. context_extras['sql_queries'] = lazy(lambda: connection.queries, list) return context_extras Loading docs/ref/templates/api.txt +2 −1 Original line number Diff line number Diff line Loading @@ -555,7 +555,8 @@ If :setting:`TEMPLATE_CONTEXT_PROCESSORS` contains this processor, every you're in :setting:`DEBUG` mode. * ``sql_queries`` -- A list of ``{'sql': ..., 'time': ...}`` dictionaries, representing every SQL query that has happened so far during the request and how long it took. The list is in order by query. and how long it took. The list is in order by query and lazily generated on access. django.core.context_processors.i18n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Loading tests/context_processors/models.py 0 → 100644 +5 −0 Original line number Diff line number Diff line from django.db import models class DebugObject(models.Model): pass tests/context_processors/templates/context_processors/debug.html 0 → 100644 +15 −0 Original line number Diff line number Diff line {% if debug == True %} Have debug First query list: {{ sql_queries|length }} {% for obj in debug_objects.all %}{{ obj }}{% endfor %} Second query list: {{ sql_queries|length }} {% for obj in debug_objects.all %}{{ obj }}{% endfor %} Third query list: {{ sql_queries|length }} {% endif %} tests/context_processors/tests.py +30 −0 Original line number Diff line number Diff line Loading @@ -33,3 +33,33 @@ class RequestContextProcessorTests(TestCase): self.assertContains(response, url) response = self.client.post(url, {'path': '/blah/'}) self.assertContains(response, url) @override_settings(ROOT_URLCONF='context_processors.urls', DEBUG=True, INTERNAL_IPS=('127.0.0.1',)) class DebugContextProcessorTests(TestCase): """ Tests for the ``django.core.context_processors.debug`` processor. """ def test_debug(self): url = '/debug/' # We should have the debug flag in the template. response = self.client.get(url) self.assertContains(response, 'Have debug') # And now we should not with override_settings(DEBUG=False): response = self.client.get(url) self.assertNotContains(response, 'Have debug') def test_sql_queries(self): """ Test whether sql_queries represents the actual amount of queries executed. (#23364) """ url = '/debug/' response = self.client.get(url) self.assertContains(response, 'First query list: 0') self.assertContains(response, 'Second query list: 1') # Check we have not actually memoized connection.queries self.assertContains(response, 'Third query list: 2') Loading
django/core/context_processors.py +6 −2 Original line number Diff line number Diff line Loading @@ -35,12 +35,16 @@ def csrf(request): def debug(request): "Returns context variables helpful for debugging." """ Returns context variables helpful for debugging. """ context_extras = {} if settings.DEBUG and request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS: context_extras['debug'] = True from django.db import connection context_extras['sql_queries'] = connection.queries # Return a lazy reference that computes connection.queries on access, # to ensure it contains queries triggered after this function runs. context_extras['sql_queries'] = lazy(lambda: connection.queries, list) return context_extras Loading
docs/ref/templates/api.txt +2 −1 Original line number Diff line number Diff line Loading @@ -555,7 +555,8 @@ If :setting:`TEMPLATE_CONTEXT_PROCESSORS` contains this processor, every you're in :setting:`DEBUG` mode. * ``sql_queries`` -- A list of ``{'sql': ..., 'time': ...}`` dictionaries, representing every SQL query that has happened so far during the request and how long it took. The list is in order by query. and how long it took. The list is in order by query and lazily generated on access. django.core.context_processors.i18n ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Loading
tests/context_processors/models.py 0 → 100644 +5 −0 Original line number Diff line number Diff line from django.db import models class DebugObject(models.Model): pass
tests/context_processors/templates/context_processors/debug.html 0 → 100644 +15 −0 Original line number Diff line number Diff line {% if debug == True %} Have debug First query list: {{ sql_queries|length }} {% for obj in debug_objects.all %}{{ obj }}{% endfor %} Second query list: {{ sql_queries|length }} {% for obj in debug_objects.all %}{{ obj }}{% endfor %} Third query list: {{ sql_queries|length }} {% endif %}
tests/context_processors/tests.py +30 −0 Original line number Diff line number Diff line Loading @@ -33,3 +33,33 @@ class RequestContextProcessorTests(TestCase): self.assertContains(response, url) response = self.client.post(url, {'path': '/blah/'}) self.assertContains(response, url) @override_settings(ROOT_URLCONF='context_processors.urls', DEBUG=True, INTERNAL_IPS=('127.0.0.1',)) class DebugContextProcessorTests(TestCase): """ Tests for the ``django.core.context_processors.debug`` processor. """ def test_debug(self): url = '/debug/' # We should have the debug flag in the template. response = self.client.get(url) self.assertContains(response, 'Have debug') # And now we should not with override_settings(DEBUG=False): response = self.client.get(url) self.assertNotContains(response, 'Have debug') def test_sql_queries(self): """ Test whether sql_queries represents the actual amount of queries executed. (#23364) """ url = '/debug/' response = self.client.get(url) self.assertContains(response, 'First query list: 0') self.assertContains(response, 'Second query list: 1') # Check we have not actually memoized connection.queries self.assertContains(response, 'Third query list: 2')