Django Template中使用含有类实例对象的字典时要注意的事项[初学者问题]
下面小弟来说下今天学Django模板时遇到的问题和解决的方法
在python idle里,我写入如下代码
class Test: id = 0 name ='' passwd = '' def __init__(self, id, name, passwd): self.id = id self.name = name self.passwd = passwd
test1 = Test(0, 'kevin', '123')test2 = Test(1, 'cong', '234')
t = Template('this is a Test, test that {{id}}')c = Context({"id":test1.id,})print t.render(c)
this is a Test, test that 0
t = Template('this is a Test, test that {{id.id}}')c = Context({"id":test1,})print t.render(c)
this is a Test, test that 0
<html><head><title></title></head><body><table > <tr> <td> {%for i in RecordInfo%} {{i.id}}<br>------------------------ {%endfor%} </td> </tr></table></body></html>
{{i.id}}
{%for i in RecordInfo.values%} {{i.id}}<br>------------------------ {%endfor%}