简体   繁体   English

在Django模板中显示字典值

[英]Displaying dictionary value in django template

All, 所有,

I have the following in my views.py 我的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>] Logging.debug的输出为[<sc: Robert>]

My question is that how do i display Robert in the template . 我的问题是我如何在模板中显示Robert。

I have tried the following in the template: <input type ="text" value={{sc}}/> //This gives me the dictionary itself 我已经在模板中尝试了以下操作: <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']) (在python上,您将其写为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. 您要在dict存储(不要将值称为dict ,您要对关键字进行屏蔽)将具有值变量sc的键“ sc”从模型中返回。 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? 也许“ Robert”存储在属性nameid或类似name

You need to show then the proper attribute, something like 您需要显示适当的属性,例如

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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