首先,我创建了可进行自动缩进的自定义过滤器:
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/