Login or Sign up

See the attributes of a variable in Django Templates

Posted by: skyl on July 7, 2010

Sometimes you want to dump the attr names of a variable so that you can remember what is in there. This isn't the best way in the world and could be improved upon but it's a start. See http://docs.djangoproject.com/en/dev/howto/custom-template-tags/ for more

The simple inclusion template tag:

from django import template

register = template.Library()

@register.inclusion_tag('portal/ttags/show_object.html')
def show_object(obj):
    attrs = [ x for x in dir(obj) if not x.startswith('_') ]
    return {'obj': obj, 'd': attrs,}

Then, in the template:

{% include mytags %}


{% show_object myobject %}

Comments on This Post:

Please Login (or Sign Up) to leave a comment