DjangoDjango

Fixing the "Data for this panel isn't available anymore" error in Django Debug Toolbar

Sep 15, 2023 · Updated: Jan 25, 2024 · by Tim Kamanin

Imagine this: you want to optimize your Django project's page loading times. To do this, you've installed the magnificent Django Debug Toolbar that will provide you with the necessary insight into how many queries your page generates. You click on the SQL panel in the Debug Toolbar, and... you get the following error message:

Data for this panel isn't available anymore. Please reload the page and retry.

You might think: "Woot? I was supposed to debug my page, not the tool!"

This happens because the page you're trying to debug generates a lot of information (potentially about loads of database queries), and the Debug Toolbar can't handle that data in its memory. Luckily, there's a RESULTS_CACHE_SIZE setting that you can increase and fix the issue.

Edit your project's settings.py and add the RESULTS_CACHE_SIZE variable. By default, its value is 25. Try to set it to at least 1000:

RESULTS_CACHE_SIZE = 1000

Reload the page and check the SQL panel. If the error persists, increase the RESULTS_CACHE_SIZE value until you find a sweet spot.

Update: October 12, 2023: It sometimes didn't work for me since sharing this tip. I looked deeper and found the problem. If the earlier solution didn't work for you, read below:

"I've increased RESULTS_CACHE_SIZE, but it didn't help me." The solution.

When the RESULTS_CACHE_SIZE increase doesn't help you, there might be another solution. Check your dev server console; you may notice a few image 404 errors. That's the source of the issue. The thing is, the Debug Toolbar bootstraps on every Django view. And when an image is missing, Django serves a 404 view. Consequently, a new Debug Toolbar instance launches in the background for each missing image. Given the numerous instances, the Debug Toolbar can't maintain a record of all the accumulated data. It discards the data from the main view, leading to the "Data for this panel isn't available anymore" error.

To resolve this, we must tell the Debug Toolbar to skip loading for /static and /media URLs. Here's how:

Edit your settings.py file and the following code to it:

if DEBUG:
    hide_toolbar_patterns = ["/media/", "/static/"]

    DEBUG_TOOLBAR_CONFIG = {
        "SHOW_TOOLBAR_CALLBACK": lambda request: not any(
            request.path.startswith(p) for p in hide_toolbar_patterns
        ),
    }

Here's a breakdown of the code:

  • if DEBUG: This checks if we're in the DEBUG mode.
  • hide_toolbar_patterns: This is a list of URL path patterns where you don't want the debug toolbar to be displayed. In this case, the patterns /media/ and /static/ are specified.
  • DEBUG_TOOLBAR_CONFIG: This dictionary configures the behavior of the Debug Toolbar.
  • SHOW_TOOLBAR_CALLBACK: This key expects a callable that takes a request object and returns True if the toolbar should be displayed for that request and False otherwise. Our lambda function then sets a rule: the debug toolbar will appear on pages unless their URL starts with either of the specified paths.

And that should solve the Django Debug Toolbar issue once and for all (fingers crossed).

Hey, if you've found this useful, please share the post to help other folks find it:

There's even more:

Subscribe for updates

  • via Twitter: @timonweb
  • old school RSS:
  • or evergreen email ↓ ↓ ↓