Skip to content

Commit 9b6aae4

Browse files
authored
Merge pull request #1440 from tim-schilling/render-panels-rework
Correct RENDER_PANELS functionality and when enabled disable HistoryPanel
2 parents 085f8dd + 8d39876 commit 9b6aae4

File tree

10 files changed

+78
-8
lines changed

10 files changed

+78
-8
lines changed

debug_toolbar/panels/history/panel.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ class HistoryPanel(Panel):
2121
nav_title = _("History")
2222
template = "debug_toolbar/panels/history.html"
2323

24+
@property
25+
def enabled(self):
26+
# Do not show the history panel if the panels are rendered on request
27+
# rather than loaded via ajax.
28+
return super().enabled and not self.toolbar.should_render_panels()
29+
2430
@property
2531
def is_historical(self):
2632
"""The HistoryPanel should not be included in the historical panels."""

debug_toolbar/templates/debug_toolbar/base.html

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,10 @@
77
<script type="module" src="{% static 'debug_toolbar/js/toolbar.js' %}" async></script>
88
{% endblock %}
99
<div id="djDebug" class="djdt-hidden" dir="ltr"
10-
{% if toolbar.store_id %}data-store-id="{{ toolbar.store_id }}" data-render-panel-url="{% url 'djdt:render_panel' %}"{% endif %}
10+
{% if not toolbar.should_render_panels %}
11+
data-store-id="{{ toolbar.store_id }}"
12+
data-render-panel-url="{% url 'djdt:render_panel' %}"
13+
{% endif %}
1114
data-default-show="{% if toolbar.config.SHOW_COLLAPSED %}false{% else %}true{% endif %}"
1215
{{ toolbar.config.ROOT_TAG_EXTRA_ATTRS|safe }}>
1316
<div class="djdt-hidden" id="djDebugToolbar">

debug_toolbar/templates/debug_toolbar/includes/panel_content.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
<h3>{{ panel.title }}</h3>
88
</div>
99
<div class="djDebugPanelContent">
10-
{% if toolbar.store_id %}
10+
{% if toolbar.should_render_panels %}
11+
<div class="djdt-scroll">{{ panel.content }}</div>
12+
{% else %}
1113
<div class="djdt-loader"></div>
1214
<div class="djdt-scroll"></div>
13-
{% else %}
14-
<div class="djdt-scroll">{{ panel.content }}</div>
1515
{% endif %}
1616
</div>
1717
</div>

debug_toolbar/toolbar.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ def render_toolbar(self):
7979
raise
8080

8181
def should_render_panels(self):
82+
"""Determine whether the panels should be rendered during the request
83+
84+
If False, the panels will be loaded via Ajax.
85+
"""
8286
render_panels = self.config["RENDER_PANELS"]
8387
if render_panels is None:
8488
render_panels = self.request.META["wsgi.multiprocess"]

docs/changes.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ Next version
55
------------
66

77
* Ensured that the handle stays within bounds when resizing the window.
8+
* Disabled ``HistoryPanel`` when ``RENDER_PANELS`` is ``True``
9+
or if ``RENDER_PANELS`` is ``None`` and the WSGI container is
10+
running with multiple processes.
11+
* Fixed ``RENDER_PANELS`` functionality so that when ``True`` panels are
12+
rendered during the request and not loaded asynchronously.
813

914

1015
3.2.1 (2021-04-14)

docs/configuration.rst

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,19 +66,24 @@ Toolbar options
6666
The toolbar searches for this string in the HTML and inserts itself just
6767
before.
6868

69+
.. _RENDER_PANELS:
70+
6971
* ``RENDER_PANELS``
7072

7173
Default: ``None``
7274

7375
If set to ``False``, the debug toolbar will keep the contents of panels in
74-
memory on the server and load them on demand. If set to ``True``, it will
75-
render panels inside every page. This may slow down page rendering but it's
76+
memory on the server and load them on demand.
77+
78+
If set to ``True``, it will disable ``HistoryPanel`` and render panels
79+
inside every page. This may slow down page rendering but it's
7680
required on multi-process servers, for example if you deploy the toolbar in
7781
production (which isn't recommended).
7882

7983
The default value of ``None`` tells the toolbar to automatically do the
8084
right thing depending on whether the WSGI container runs multiple processes.
81-
This setting allows you to force a different behavior if needed.
85+
This setting allows you to force a different behavior if needed. If the
86+
WSGI container runs multiple processes, it will disable ``HistoryPanel``.
8287

8388
* ``RESULTS_CACHE_SIZE``
8489

docs/installation.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,11 @@ And for Apache:
147147
.. _JavaScript module: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
148148
.. _CORS errors: https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS/Errors/CORSMissingAllowOrigin
149149
.. _Access-Control-Allow-Origin header: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Access-Control-Allow-Origin
150+
151+
Django Channels & Async
152+
^^^^^^^^^^^^^^^^^^^^^^^
153+
154+
The Debug Toolbar currently doesn't support Django Channels or async projects.
155+
If you are using Django channels are having issues getting panels to load,
156+
please review the documentation for the configuration option
157+
:ref:`RENDER_PANELS <RENDER_PANELS>`.

docs/panels.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ History
1717
This panel shows the history of requests made and allows switching to a past
1818
snapshot of the toolbar to view that request's stats.
1919

20+
.. caution::
21+
If :ref:`RENDER_PANELS <RENDER_PANELS>` configuration option is set to
22+
``True`` or if the server runs with multiple processes, the History Panel
23+
will be disabled.
24+
2025
Version
2126
~~~~~~~
2227

docs/spelling_wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ unhashable
3737
uWSGI
3838
validator
3939
Werkzeug
40+
async

tests/test_integration.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,44 @@ def test_sql_profile_checks_show_toolbar(self):
321321
self.assertEqual(response.status_code, 404)
322322

323323
@override_settings(DEBUG_TOOLBAR_CONFIG={"RENDER_PANELS": True})
324-
def test_data_store_id_not_rendered_when_none(self):
324+
def test_render_panels_in_request(self):
325+
"""
326+
Test that panels are are rendered during the request with
327+
RENDER_PANELS=TRUE
328+
"""
325329
url = "/regular/basic/"
326330
response = self.client.get(url)
327331
self.assertIn(b'id="djDebug"', response.content)
332+
# Verify the store id is not included.
328333
self.assertNotIn(b"data-store-id", response.content)
334+
# Verify the history panel was disabled
335+
self.assertIn(
336+
b'<input type="checkbox" data-cookie="djdtHistoryPanel" '
337+
b'title="Enable for next and successive requests">',
338+
response.content,
339+
)
340+
# Verify the a panel was rendered
341+
self.assertIn(b"Response headers", response.content)
342+
343+
@override_settings(DEBUG_TOOLBAR_CONFIG={"RENDER_PANELS": False})
344+
def test_load_panels(self):
345+
"""
346+
Test that panels are not rendered during the request with
347+
RENDER_PANELS=False
348+
"""
349+
url = "/execute_sql/"
350+
response = self.client.get(url)
351+
self.assertIn(b'id="djDebug"', response.content)
352+
# Verify the store id is included.
353+
self.assertIn(b"data-store-id", response.content)
354+
# Verify the history panel was not disabled
355+
self.assertNotIn(
356+
b'<input type="checkbox" data-cookie="djdtHistoryPanel" '
357+
b'title="Enable for next and successive requests">',
358+
response.content,
359+
)
360+
# Verify the a panel was not rendered
361+
self.assertNotIn(b"Response headers", response.content)
329362

330363
def test_view_returns_template_response(self):
331364
response = self.client.get("/template_response/basic/")

0 commit comments

Comments
 (0)