python 3.x - accessing objects stored in dictionary as values using django templatetag outputs only its string representation; not original object -


in views.py

i building dictionary strings (a1, a2, a3....c10) keys , database objects values.

itemsbyposition = {} storage_instance in storageinstance.objects.all():     itemsbyposition[storage_instance.cell] = storage_instance 

in template,

i display different fields objects. isolated, hardcoded example (in reality use loops print entire dictionary):

{{ itemsbyposition|hash:'a2' }} 

in templatetags.py

def hash(h,key):     if key in h:         return h[key]     else:        return none  register.filter(hash) 

however, template tag 'hash' returns string representation of model (eg: "stabi00052") not entire object. want able access various attributes of object in template.

you can access dictionary values directly without custom filter if key string without spaces or punctuation other underscores (documentation)

{{ itemsbyposition.a2.attribute }} 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -