from django import template
register = template.Library()

@register.simple_tag
def get_obj(pk, attr):
    obj = getattr(A.objects.get(pk=int(pk)), attr)
return obj
How to get object from PK inside Django template?