繁体   English   中英

在Django模板中显示字典值

[英]Displaying dictionary value in django template

所有,

我的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的输出为[<sc: Robert>]

我的问题是我如何在模板中显示Robert。

我已经在模板中尝试了以下操作: <input type ="text" value={{sc}}/> //This gives me the dictionary itself

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

谢谢......

如果您想要字典中的任何值,则必须顺便去做

dict.key

(在python上,您将其写为dict ['key'])

因此,要显示用键“名称”存储的值

{{ sc.name }}

无论如何,我认为事实并非如此。 我认为您没有看到字典,而是从模型定义的对象(数据库中的条目也是如此)。 您要在dict存储(不要将值称为dict ,您要对关键字进行屏蔽)将具有值变量sc的键“ sc”从模型中返回。 我不得不猜测,因为我不知道该模型如何。 也许“ Robert”存储在属性nameid或类似name

您需要显示适当的属性,例如

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

暂无
暂无

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

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