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

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -