[英]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”存儲在屬性name
, id
或類似name
?
您需要顯示適當的屬性,例如
{{ sc.name }}
{{ sc.id }}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.