首页 > 解决方案 > '|' 是什么意思 在 {{ services|pprint|safe }} 在 django 中是什么意思?

问题描述

我知道在 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 }}

标签: djangotemplates

解决方案


它们被称为内置标签/过滤器

基本上,它接受视图传递给它的变量并以某种方式处理它。该| 表示左边的数据要传到右边,这是一个返回值的函数。

pprint是 Python 中 pprint 的包装器,它是一个很好地将数据结构打印到控制台的函数。

安全与 HTML 转义有关。本质上,它告诉 Django 内容在输出到模板之前不需要进一步处理。


推荐阅读