Django 模板
我正在做一个关于模板的 Django 教程。我目前在这段代码中:
from django.template import Template, Context
>>> person = {'name': 'Sally', 'age': '43'}
>>> t = Template('{{ person.name }} is {{ person.age }} years old.')
>>> c = Context({'person': person})
>>> t.render(c)
u'Sally is 43 years old.'
我不明白的是这一行:
c = Context({'person': person})
两个变量都需要被称为 person 才能在这个示例中使用,还是只是随机的?
'person'
指代什么,person
指代什么?
I am doing a Django tutorial on Templates. I am currently at this code:
from django.template import Template, Context
>>> person = {'name': 'Sally', 'age': '43'}
>>> t = Template('{{ person.name }} is {{ person.age }} years old.')
>>> c = Context({'person': person})
>>> t.render(c)
u'Sally is 43 years old.'
What I don't understand is this line:
c = Context({'person': person})
Do both variables need to be called person to be used in this example or is it just random?
What is 'person'
referring to and what is person
referring to?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第一人称(引号内)表示
Template
所需的变量名称。第二个人将代码第二行中创建的person
变量分配给要传递给Context
的 person 变量>模板。第二个可以是任何内容,只要与其声明匹配即可。这应该可以澄清一些事情:
The first person (within quotes) denotes the variable name that the
Template
expects. The second person assigns theperson
variable created in the second line of your code to the person variable of theContext
to be passed to theTemplate
. The second one can be anything, as long as it matches with its declaration.This should clarify things a little bit:
不,这只是随机的。
首先,
{}
是一个字典对象,它是关联数组或哈希的 Python 术语。它基本上是一个具有(几乎)任意键的数组。因此,在您的示例中,
'person'
是键,person
是值。当此字典传递到模板时,您可以使用您之前选择的键来访问您的真实对象(此处为人物,包括姓名、年龄等)。
作为替代示例:
No, this is just random.
First, the
{}
is a dictionary object, which is the python terminology for an associative array or hash. It is basically an array with (almost) arbitrary keys.So in your example,
'person'
would be the key,person
the value.When this dictionary gets passed to the template, you can access your real objects (here, the person, with name, age, etc) by using the key, you choose before.
As an alternative example:
{'person': person}
是标准的 Python dict< /a>.Context
构造函数采用一个字典并生成一个适合在模板中使用的上下文对象。Template.render()
方法是您将上下文传递给模板并接收最终结果的方法。{'person': person}
is a standard Python dict. TheContext
constructor takes a dict and produces a context object suitable for use in a template. TheTemplate.render()
method is how you pass the context to the template, and receive the final result.c = Context({'person': person})
这里字典中的第一个“人”是变量名(键),而其他人代表您在上面行中声明的变量,即
t = Template('{{ Student.name }} 今年 {{ Student.age }} 岁。')
Context 是一个构造函数,它采用一个可选参数 &它是一个将变量名称映射到变量值的字典。使用上下文调用 Template 对象的 render() 方法来“填充”模板:
要获取更多信息,请访问给定的链接
http://www.djangobook.com/en/2.0/chapter04.html
c = Context({'person': person})
Here the first 'person' in dictionary is variable name(key) where as other person represent the variable which you have declared in above line i.e.
t = Template('{{ student.name }} is {{ student.age }} years old.')
Context is a constructor,that takes one optional argument & its a dictionary mapping variable names to variable values. Call the Template object’s render() method with the context to “fill” the template:
to get more information visit given link
http://www.djangobook.com/en/2.0/chapter04.html