Login

Updated - Template context debugger with (I)Pdb

Author:
dnordberg
Posted:
August 19, 2009
Language:
Python
Version:
1.1
Tags:
template debug context pdb ipdb
Score:
3 (after 3 ratings)

Tag to inspect template context, filter to inspect variable.

Originally by denis, http://www.djangosnippets.org/snippets/1550/.

This just extracts variables from the context to locals for quicker access and autocompletion (When using ipdb).

Additionally, 'vars' holds context keys for quick reference to whats available in the template.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
from django.template import Library, Node

register = Library()

try:
    import ipdb as pdb
except ImportError:
    import pdb


class PdbNode(Node):
    def render(self, context):
        # Access vars at the prompt for an easy reference to
        # variables in the context
        vars = []
        for d in context.dicts:
            for k, v in d.items():
                vars.append(k)
                locals()[k] = v
        del v
        del k
        del d
        pdb.set_trace()
        return ''


@register.tag("pdbdebug")
def pdbdebug_tag(parser, token):
    """Tag that inspects template context.

    Usage:
    {% pdbdebug %}

    You can then access your context variables directly at the prompt.

    The vars variable additonally has a reference list of keys
    in the context.
    """
    return PdbNode()


@register.filter("pdbdebug")
def pdbdebug_filter(value, arg=None):
    """Filter that inspects a specific
    variable in context.

    Usage:
    {{ variable|pdbdebug }}
    """
    pdb.set_trace()
    return ''

More like this

  1. Template context debugger with (I)Pdb by denis 6 years, 1 month ago
  2. Manipulate URL query strings using context variables using a template tag by JHsaunders 4 years, 8 months ago
  3. Inspect object debugging tag by dballanc 7 years, 12 months ago
  4. DebugFooter middleware with syntax highlighting and code inspection by monolar 7 years ago
  5. Load response.content in browser (for debugging) by tin_nqn 3 years, 1 month ago

Comments

Please login first before commenting.