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