Performing a getattr() style lookup in a django template

PythonDjangoDjango Templates

Python Problem Overview


Python's getattr() method is useful when you don't know the name of a certain attribute in advance.

This functionality would also come in handy in templates, but I've never figured out a way to do it. Is there a built-in tag or non-built-in tag that can perform dynamic attribute lookups?

Python Solutions


Solution 1 - Python

I also had to write this code as a custom template tag recently. To handle all look-up scenarios, it first does a standard attribute look-up, then tries to do a dictionary look-up, then tries a getitem lookup (for lists to work), then follows standard Django template behavior when an object is not found.

(updated 2009-08-26 to now handle list index lookups as well)

# app/templatetags/getattribute.py

import re
from django import template
from django.conf import settings

numeric_test = re.compile("^\d+$")
register = template.Library()

def getattribute(value, arg):
	"""Gets an attribute of an object dynamically from a string name"""
	
	if hasattr(value, str(arg)):
		return getattr(value, arg)
	elif hasattr(value, 'has_key') and value.has_key(arg):
		return value[arg]
	elif numeric_test.match(str(arg)) and len(value) > int(arg):
		return value[int(arg)]
	else:
		return settings.TEMPLATE_STRING_IF_INVALID

register.filter('getattribute', getattribute)

Template usage:

{% load getattribute %}
{{ object|getattribute:dynamic_string_var }}


Solution 2 - Python

I don't think so. But it wouldn't be too hard to write a custom template tag to return an attribute in the context dict. If you're simply trying to return a string, try something like this:

class GetAttrNode(template.Node):
    def __init__(self, attr_name):
        self.attr_name = attr_name

    def render(self, context):
        try:
            return context[self.attr_name]
        except:
            # (better yet, return an exception here)
            return ''

@register.tag
def get_attr(parser, token):
    return GetAttrNode(token)

Note that it's probably just as easy to do this in your view instead of in the template, unless this is a condition that is repeated often in your data.

Solution 3 - Python

I ended up adding a method to the model in question, and that method can be accessed like an attribute in the template.

Still, i think it would be great if a built in tag allowed you to dynamically lookup an attribute, since this is a problem a lot of us constantly have in our templates.

Solution 4 - Python

Keeping the distinction between get and getattr,

@register.filter(name='get')
def get(o, index):
    try:
        return o[index]
    except:
        return settings.TEMPLATE_STRING_IF_INVALID


@register.filter(name='getattr')
def getattrfilter(o, attr):
    try:
        return getattr(o, attr)
    except:
        return settings.TEMPLATE_STRING_IF_INVALID

Solution 5 - Python

There isn't a built-in tag, but it shouldn't be too difficult to write your own.

Solution 6 - Python

That snippet saved my day but i needed it to span it over relations so I changed it to split the arg by "." and recursively get the value. It could be done in one line: return getattribute(getattribute(value,str(arg).split(".")[0]),".".join(str(arg).split(".")[1:])) but I left it in 4 for readability. I hope someone has use for this.

import re
from django import template
from django.conf import settings
 
numeric_test = re.compile("^\d+$")
register = template.Library()

def getattribute(value, arg):
"""Gets an attribute of an object dynamically AND recursively from a string name"""
    if "." in str(arg):
        firstarg = str(arg).split(".")[0]
        value = getattribute(value,firstarg)
        arg = ".".join(str(arg).split(".")[1:])
        return getattribute(value,arg)
    if hasattr(value, str(arg)):
        return getattr(value, arg)
    elif hasattr(value, 'has_key') and value.has_key(arg):
        return value[arg]
    elif numeric_test.match(str(arg)) and len(value) > int(arg):
        return value[int(arg)]
    else:
        #return settings.TEMPLATE_STRING_IF_INVALID
        return 'no attr.' + str(arg) + 'for:' + str(value)

register.filter('getattribute', getattribute)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionjamtodayView Question on Stackoverflow
Solution 1 - PythonfotinakisView Answer on Stackoverflow
Solution 2 - PythonDaniel NaabView Answer on Stackoverflow
Solution 3 - PythonjamtodayView Answer on Stackoverflow
Solution 4 - PythonSkylar SavelandView Answer on Stackoverflow
Solution 5 - PythonmipadiView Answer on Stackoverflow
Solution 6 - PythonVolkerView Answer on Stackoverflow