Login

Persistent Session Debugging with Django Debug Toolbar

Author:
brianjaystanley
Posted:
October 19, 2011
Language:
Python
Version:
1.3
Tags:
session debug debug-toolbar
Score:
1 (after 1 ratings)

When using django debug toolbar, I like to be able to turn debugging on and off without having to edit my settings file. This callback makes that possible. Add ?debug=on to the URL to turn debugging on. It will remain on in the current session until you turn it off with ?debug=off.

Make sure your session middleware comes before your debug toolbar middleware.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
MIDDLEWARE_CLASSES = [
    ...
    "django.contrib.sessions.middleware.SessionMiddleware",
    "debug_toolbar.middleware.DebugToolbarMiddleware",
    ...
]

def show_debug_toolbar(request):
    """Shows the debug toolbar based on the debug arg in the URL.
    
    ?debug=on - turns debugging on in the current session
    ?debug=off - turns debugging off in the current session
    """
    if DEBUG:
        debug = request.GET.get("debug", None)
        if debug == "on":
            request.session["debug"] = True
        elif debug == "off" and "debug" in request.session:
            del request.session["debug"]
        return "debug" in request.session
    
DEBUG_TOOLBAR_CONFIG = {
    "SHOW_TOOLBAR_CALLBACK": show_debug_toolbar,
}

More like this

  1. Custom panel showing your project svn revision on djano debug toolbar by coulix 6 years, 7 months ago
  2. CurrentSessionIDMiddleware by troolee 4 years, 9 months ago
  3. Globs for INTERNAL_IPS by kcarnold 6 years, 1 month ago
  4. Include entire networks in INTERNAL_IPS setting by pmclanahan 6 years, 1 month ago
  5. Smart Spaceless by btaylordesign 4 years ago

Comments

Please login first before commenting.