我知道在views.py文件中:
def index(request):
person= {'firstname': 'Craig', 'lastname': 'Daniels'}
weather= "sunny"
context= {
'person': person,
'weather': weather,
}
return render(request, 'Articles/greeting.html', context)
然后我们可以在greetings.html中做:
<h1>Hi {{ person.firstname }} {{ person.lastname }}</h1>
<h1>Today it is {{ weather }}</h1>
其中 {{ person.firstname }} 是上下文中定义的变量。
但是“|”是什么意思意思是?
{{ services|pprint|safe }}
最佳答案
它们被称为 built in tags/filters
基本上,它获取 View 传递给它的变量并以某种方式处理它。 | 的 |表示左边的数据要传递到右边,这是一个有返回值的函数。
pprint是 Python 中 pprint 的包装器,它是一个将数据结构很好地打印到控制台的函数。
safe与 HTML 转义有关。本质上,它告诉 Django 内容在输出到模板之前不需要进一步处理。
关于django - {{ services|pprint|safe }} 中的 '|' 在 django 中意味着什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57932297/