|
| 1 | +Architecture |
| 2 | +============ |
| 3 | + |
| 4 | +The Django Debug Toolbar is designed to be flexible and extensible for |
| 5 | +developers and third-party panel creators. |
| 6 | + |
| 7 | +Core Components |
| 8 | +--------------- |
| 9 | + |
| 10 | +While there are several components, the majority of logic and complexity |
| 11 | +lives within the following: |
| 12 | + |
| 13 | +- ``debug_toolbar.middleware.DebugToolbarMiddleware`` |
| 14 | +- ``debug_toolbar.toolbar.DebugToolbar`` |
| 15 | +- ``debug_toolbar.panels`` |
| 16 | + |
| 17 | +^^^^^^^^^^^^^^^^^^^^^^ |
| 18 | +DebugToolbarMiddleware |
| 19 | +^^^^^^^^^^^^^^^^^^^^^^ |
| 20 | + |
| 21 | +The middleware is how the toolbar integrates with Django projects. |
| 22 | +It determines if the toolbar should instrument the request, which |
| 23 | +panels to use, facilitates the processing of the request and augmenting |
| 24 | +the response with the toolbar. Most logic for how the toolbar interacts |
| 25 | +with the user's Django project belongs here. |
| 26 | + |
| 27 | +^^^^^^^^^^^^ |
| 28 | +DebugToolbar |
| 29 | +^^^^^^^^^^^^ |
| 30 | + |
| 31 | +The ``DebugToolbar`` class orchestrates the processing of a request |
| 32 | +for each of the panels. It contains the logic that needs to be aware |
| 33 | +of all the panels, but doesn't need to interact with the user's Django |
| 34 | +project. |
| 35 | + |
| 36 | +^^^^^^ |
| 37 | +Panels |
| 38 | +^^^^^^ |
| 39 | + |
| 40 | +The majority of the complex logic lives within the panels themselves. This |
| 41 | +is because the panels are responsible for collecting the various metrics. |
| 42 | +Some of the metrics are collected via |
| 43 | +`monkey-patching <https://stackoverflow.com/a/5626250>`_, such as |
| 44 | +``TemplatesPanel``. Others, such as ``SettingsPanel`` don't need to collect |
| 45 | +anything and include the data directly in the response. |
| 46 | + |
| 47 | +Some panels such as ``SQLPanel`` have additional functionality. This tends |
| 48 | +to involve a user clicking on something, and the toolbar presenting a new |
| 49 | +page with additional data. That additional data is handled in views defined |
| 50 | +in the panels package (for example, ``debug_toolbar.panels.sql.views``). |
| 51 | + |
| 52 | +Logic Flow |
| 53 | +---------- |
| 54 | + |
| 55 | +When a request comes in, the toolbar first interacts with it in the |
| 56 | +middleware. If the middleware determines the request should be instrumented, |
| 57 | +it will instantiate the toolbar and pass the request for processing. The |
| 58 | +toolbar will use the enabled panels to collect information on the request |
| 59 | +and/or response. When the toolbar has completed collecting its metrics on |
| 60 | +both the request and response, the middleware will collect the results |
| 61 | +from the toolbar. It will inject the HTML and JavaScript to render the |
| 62 | +toolbar as well as any headers into the response. |
| 63 | + |
| 64 | +After the browser renders the panel and the user interacts with it, the |
| 65 | +toolbar's JavaScript will send requests to the server. If the view handling |
| 66 | +the request needs to fetch data from the toolbar, the request must supply |
| 67 | +the store ID. This is so that the toolbar can load the collected metrics |
| 68 | +for that particular request. |
| 69 | + |
| 70 | +The history panel allows a user to view the metrics for any request since |
| 71 | +the application was started. The toolbar maintains its state entirely in |
| 72 | +memory for the process running ``runserver``. If the application is |
| 73 | +restarted the toolbar will lose its state. |
| 74 | + |
| 75 | +Problematic Parts |
| 76 | +----------------- |
| 77 | + |
| 78 | +- ``debug.panels.templates.panel``: This monkey-patches template rendering |
| 79 | + when the panel module is loaded |
| 80 | +- ``debug.panels.sql``: This package is particularly complex, but provides |
| 81 | + the main benefit of the toolbar |
| 82 | +- Support for async and multi-threading: This is currently unsupported, but |
| 83 | + is being implemented as per the |
| 84 | + `Async compatible toolbar project <https://github.com/orgs/jazzband/projects/9>`_. |
0 commit comments