gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 18:21:49 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com