Login

Persistent Session Debugging with Django Debug Toolbar

Author:
brianjaystanley
Posted:
October 19, 2011
Language:
Python
Version:
1.3
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. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.