python - 我可以在 Django 1.11 中将 {% include %} 与自定义过滤器结合使用吗?

标签 python django templates

首先,我创建了可进行自动缩进的自定义过滤器:

templatetags/indent.py

from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()

@register.filter(name='indent')
@stringfilter
def indent(value, arg):
    import re
    regex = re.compile('^', re.M)
    return re.sub(regex, ' ' * int(arg), value)

我想包含将缩进一定数量的空格(例如 8)的子模板:

在 django 的模板中是否允许这样做:

{% load indent %}

{% include "footer.html"|indent:"8" %}

最佳答案

我认为您可以使用 filter 来做到这一点:

{% load indent %}

{% filter indent:"8" %}
    {% include "footer.html" %}
{% endfilter %}

关于python - 我可以在 Django 1.11 中将 {% include %} 与自定义过滤器结合使用吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44685126/

相关文章:

python - 以 10 为基数的 int() 的文字无效 : 'marlm'

python - 可以使用 Python 删除 Elastic Search 中的特定文档吗

python - Django - 将联系表单添加到页脚

c++ - 一种用于 POD 的模板特化,一种用于类层次结构和其他情况下的错误?

c++ - 当它被称为 shared_ptr<Basic> 时调用派生类方法

python - 为什么在 Pandas 中使用 apply 时会有一个额外的索引

python - 通过按下另一个屏幕中的按钮来更改 Kivy 按钮小部件属性

python - 使用jinja传递的上下文中的键访问jinja2中dict中的元素

Django,如何以自定义形式使用管理自动填充字段

c++ - 如何在编译时检查函数是否在全局范围内声明