django - {{ services|pprint|safe }} 中的 '|' 在 django 中意味着什么?

标签 django templates

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

相关文章:

c++ - 编译类型模板谓词使用 Clang 进行编译,但不使用 GCC 或 MSVC

python - 如何在 Django 中编写一个以 URL 作为参数的自定义管理命令?

python - swfupload 和 django 上的 403 禁止错误

python - Django manytomany 关系超过 2 个应用程序

html - Django 模板空 div 中插入空格

c++ - 模板和继承

c++ - 使用模板C++重载运算符

python - Django ORM如何获取按字段分组的原始值

c++ - 模板 std::function 作为类成员函数 C++

c++ - 在程序中使用非类型模板参数而不分配给局部变量时出现意外结果?