Django 模板

发布于 2024-09-06 20:21:03 字数 523 浏览 5 评论 0原文

我正在做一个关于模板的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

紫罗兰の梦幻 2024-09-13 20:21:03
c = Context({'person': person})

第一人称(引号内)表示 Template 所需的变量名称。第二个人将代码第二行中创建的 person 变量分配给要传递给 Contextperson 变量>模板。第二个可以是任何内容,只要与其声明匹配即可。

这应该可以澄清一些事情:

from django.template import Template, Context
>>> someone = {'name': 'Sally', 'age': '43'}
>>> t = Template('{{ student.name }} is {{ student.age }} years old.')
>>> c = Context({'student': someone})
>>> t.render(c)
c = Context({'person': person})

The first person (within quotes) denotes the variable name that the Template expects. The second person assigns the person variable created in the second line of your code to the person variable of the Context to be passed to the Template. The second one can be anything, as long as it matches with its declaration.

This should clarify things a little bit:

from django.template import Template, Context
>>> someone = {'name': 'Sally', 'age': '43'}
>>> t = Template('{{ student.name }} is {{ student.age }} years old.')
>>> c = Context({'student': someone})
>>> t.render(c)
心如狂蝶 2024-09-13 20:21:03

在本例中使用的两个变量都需要称为 person 还是只是随机的?

不,这只是随机的。

“人”指的是什么?人指的是什么?

首先,{} 是一个字典对象,它是关联数组或哈希的 Python 术语。它基本上是一个具有(几乎)任意键的数组。

因此,在您的示例中,'person' 是键,person 是值。

当此字典传递到模板时,您可以使用您之前选择的键来访问您的真实对象(此处为人物,包括姓名、年龄等)。

作为替代示例:

# we just use another key here (x)
c = Context({'x': person})

# this would yield the same results as the original example
t = Template('{{ x.name }} is {{ x.age }} years old.')

Do both variables need to be called person to be used in this example or is it just random?

No, this is just random.

What is 'person' refering to and what is person refering to?

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:

# we just use another key here (x)
c = Context({'x': person})

# this would yield the same results as the original example
t = Template('{{ x.name }} is {{ x.age }} years old.')
水染的天色ゝ 2024-09-13 20:21:03

{'person': person} is a standard Python dict. The Context constructor takes a dict and produces a context object suitable for use in a template. The Template.render() method is how you pass the context to the template, and receive the final result.

晨曦慕雪 2024-09-13 20:21:03

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文