Login

Updated - Template context debugger with (I)Pdb

Author:
dnordberg
Posted:
August 19, 2009
Language:
Python
Version:
1.1
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. Django Settings Assignment Expressions aka Walrus Operator example by webology 2 weeks, 5 days ago
  2. codigo alto nivel by MrRocklion 2 months, 2 weeks ago
  3. Load template from specific app by Krzysiek555 3 months, 1 week ago
  4. PostgreSQL JSON subqueries by dolamroth 3 months, 1 week ago
  5. "Magic Link" Management Command by webology 8 months, 1 week ago

Comments

Please login first before commenting.