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

Comments

Please login first before commenting.