Login

Template context debugger with (I)Pdb

Author:
denis
Posted:
June 4, 2009
Language:
Python
Version:
1.0
Tags:
template debug context pdb ipdb
Score:
7 (after 7 ratings)

This allows you to set up a breakpoint anywhere in your template code, by simply writing {% pdb_debug %}.

You can then access your context variables using context.get(..) at the pdb prompt. Optionally, install the ipdb package for colors, completion, and more (easy_install ipdb).

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
from django.template import Library, Node

register = Library()

try:
    import ipdb as pdb
except ImportError:   
    import pdb

class PdbNode(Node):
    def render(self, context):
        pdb.set_trace()
        return ''
@register.tag
def pdb_debug(parser, token):
    return PdbNode()

More like this

Comments

showell (on June 4, 2009):
<p>This is an interesting idea. I am using pydev now, and I'm relatively new to that environment, and I am wondering if there's a similar way to hook into pydev's debugger.</p>

#

jsandell (on June 5, 2009):
<p>showell: I don't know if this has changed, as I haven't used Eclipse for a couple of years, but from what I can recall all you have to do is make sure the pydev directory is in your Django application's python path. i.e., put this:</p> <pre>import sys; sys.path.insert(0, '/path/to/pydev') </pre> <p>below the line:</p> <pre>from django.template import Library, Node </pre> <p>in this snippet.</p> <p>Hope this helps.</p> <p>(Note, I bet it'll work with WinPdb as well)</p>

#

Please login first before commenting.