简体   繁体   中英

Displaying dictionary value in django template

All,

I have the following in my views.py

def getgradeform(request):
   id1=request.user.get_pf().id
   sc=Sc.objects.filter(id=id1)
   logging.debug(sc)
   logging.debug("++++")
   dict={}
   dict.update({'sc': sc})
   return render_to_response('content/add.html',dict)

Logging.debug gives an output as [<sc: Robert>]

My question is that how do i display Robert in the template .

I have tried the following in the template: <input type ="text" value={{sc}}/> //This gives me the dictionary itself

<input type ="text" value={{dict.sc}}/> //This also doesnt work.

Thanks......

If you want any value in a dictionary, you have to do it on the way

dict.key

(On python you'll write it as dict['key'])

So, to present the value stored with key 'name'

{{ sc.name }}

Anyway, I think this is not you're case. I think you're not seeing a dictionary, but an object defined from models (as is a entry on the database). You're storing in dict (don't call that value as dict , you're masking a keyword) a key 'sc' with value variable sc , which is returned from a model. I'm having to guess, because I don't know how this model is. Maybe 'Robert' is stored in the attribute name , id or something similar?

You need to show then the proper attribute, something like

{{ sc.name }}
{{ sc.id }}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM