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
- Serializer factory with Django Rest Framework by julio 3 months, 1 week ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 3 months, 4 weeks ago
- Help text hyperlinks by sa2812 4 months, 3 weeks ago
- Stuff by NixonDash 7 months ago
- Add custom fields to the built-in Group model by jmoppel 9 months ago
Comments
Please login first before commenting.