Login

Inspect object debugging tag

Author:
dballanc
Posted:
July 9, 2007
Language:
Python
Version:
.96
Tags:
tag debug debugging introspection
Score:
6 (after 6 ratings)

Simple template tag that allows you to inspect an object in the context for debugging. It will inspect django models and forms using introspection. Dicts and lists are formatted to truncate long data. Pretty much everything else just displays the str representation + class name.

Example output: http://dsanity.net/introspectiontag.html

Usage:

put tag code as file introspection.py in your templatetags directory. Place the html template in your template path under "inspect_object.html".

Then in your template:

{% load introspection %}

{% inspect_object obj "test object" %}

The first parameter is the object to inspect, the second is the name used for display purposes.

  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
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from django import template

register = template.Library()

class Info(object):
    pass

def fixup(text):
    t = str(text)
    if len(t) > 30:
        return str(t)[:27]+"..."
    else:
        return t
    
def fixuplist(l):
    nl = []
    for i in range(0,len(l)):
        nl.append(fixup(l[i]))
    return nl

def inspect_model_instance(instance):
    mm = instance.__class__._meta
    model = instance.__class__.__name__
    fields = []
    for f in mm.fields + mm.many_to_many:
        field = Info()
        field.name = f.name
        field.type = f.__class__.__name__
        field.value = fixup(getattr(instance,f.name))
        fields.append(field)
    return {'model': model, 'fields': fields }

def inspect_form_instance(instance):
    fields = []
    bound = str(instance.is_bound)
    for f in instance.fields.keys():
        field = Info()
        field.name = f
        field.type = instance.fields[f].__class__.__name__
        field.widget = instance.fields[f].widget.__class__.__name__
        field.initial = fixup(instance.initial.get(f,''))
        field.bound = fixup(instance.data.get(f,''))
        field.errors = fixuplist(instance[f].errors)
        fields.append(field)
    return {'bound': bound, 'fields': fields }

def inspect_dict(instance):
    keys = []
    for k in instance.keys():
        key = Info()
        key.name = k
        key.type = instance[k].__class__.__name__
        key.value = fixup(instance[k])
        keys.append(key)
    return {'keys': keys }

def inspect_object(obj,name=""):
    dtype = 'basic'
    classname = obj.__class__.__name__
    meta = fixup(obj) 
    if getattr(obj,'__metaclass__',None):
        if obj.__metaclass__.__name__ == 'DeclarativeFieldsMetaclass': 
            dtype = 'form'
            meta = inspect_form_instance(obj)
        elif obj.__metaclass__.__name__ == 'ModelBase': 
            dtype = 'model'
            meta = inspect_model_instance(obj)
    elif classname == 'dict':
        dtype = 'dict'
        meta = inspect_dict(obj)
    elif classname == 'list':
        dtype = 'list'
        meta = fixuplist(obj)
        
    return {'classname': classname,'name': name, 'dtype': dtype, 'meta': meta}

register.inclusion_tag('inspect_object.html')(inspect_object)

"""  Template goes in file: inspect_object.html

<table style="border: 1px solid;background-color: green">
 <tr style="background-color:#e2b753"><th>{{name}}({{classname}})</th></tr>
{% ifequal dtype "basic" %}
 <tr style="background-color:#F9FBCE"><td style="padding:4px;">{{meta}}</td></tr>
{% endifequal %}
{% ifequal dtype "model" %}
 <tr>
  <td>
   <table style="background-color: green" cellpadding="4px">
    <tr><th>Field</th><th>Type</th><th>Value</th></tr>
    {% for f in meta.fields %}
    <tr style="background-color:{% cycle #F9FBCE,#E1E2D5 %}">
        <td>{{ f.name }}</td>
        <td>{{ f.type }}</td>
        <td>{{ f.value }}</td>
    </tr>
    {% endfor %}
   </table>
  </td>
 </tr>
{% endifequal %}
{% ifequal dtype "form" %}
 <tr>
  <td>
   <table style="background-color: green;" cellpadding="4px">
    <tr>
        <th>Field</th>
        <th>Type</th>
        <th>Widget</th>
        <th>Initial</th>
        <th>Bound</th>
        <th>Errors</th>
    </tr>
    {% for f in meta.fields %}
    <tr style="background-color:{% cycle #F9FBCE,#E1E2D5 %}">
        <td>{{ f.name }}</td>
        <td>{{ f.type }}</td>
        <td>{{ f.widget }}</td>
        <td>{{ f.initial }}</td>
        <td>{{ f.bound }}</td>
	<td>
 	{% for e in f.errors %}
	{{ e }}
	{% endfor %}
	</td>
    </tr>
    {% endfor %}
   </table>
  </td>
 </tr>
{% endifequal %}
{% ifequal dtype "dict" %}
 <tr>
  <td>
   <table style="background-color: green" cellpadding="4px">
    <tr><th>Key</th><th>Type</th><th>Value</th></tr>
    {% for k in meta.keys %}
    <tr style="background-color:{% cycle #F9FBCE,#E1E2D5 %}">
        <td>{{ k.name }}</td>
        <td>{{ k.type }}</td>
        <td>{{ k.value }}</td>
    </tr>
    {% endfor %}
   </table>
  </td>
 </tr>
{% endifequal %}
{% ifequal dtype "list" %}
 <tr>
  <td>
   <table style="background-color: green" cellpadding="4px">
    {% for i in meta %}
    <tr style="background-color:{% cycle #F9FBCE,#E1E2D5 %}">
        <td>{{ i }}</td>
    </tr>
    {% endfor %}
   </table>
  </td>
 </tr>
{% endifequal %}
</table>

"""

    

More like this

  1. Updated - Template context debugger with (I)Pdb by dnordberg 5 years, 9 months ago
  2. Manager introspecting attached model by ubernostrum 7 years, 3 months ago
  3. Debug view: show available named URL patterns by simon 6 years, 1 month ago
  4. Mako support as decorator by rmt 7 years, 6 months ago
  5. Render arbitrary models - template tag by jeffschenck 5 years, 8 months ago

Comments

Please login first before commenting.