Python Django-模板标记内变量的字符串

Python Django-模板标记内变量的字符串,python,django,django-templates,django-template-filters,Python,Django,Django Templates,Django Template Filters,我不知道如何将多个参数发送到自定义模板筛选器 问题是我使用模板变量作为参数 自定义模板过滤器 @register.filter def is_scheduled(product_id,dayhour): day,hour = dayhour.split(',') return Product.objects.get(id=product_id).is_scheduled(day,hour) 正常使用 {% if product.id|is_scheduled:"7,22" %}

我不知道如何将多个参数发送到自定义模板筛选器

问题是我使用模板变量作为参数

自定义模板过滤器

@register.filter
def is_scheduled(product_id,dayhour):
    day,hour = dayhour.split(',')
    return Product.objects.get(id=product_id).is_scheduled(day,hour)
正常使用

{% if product.id|is_scheduled:"7,22" %}...{% endif %}
上面这一行可以正常工作,就像我将两个参数-7和22放入过滤器一样(tested-works)。问题是我想把变量而不是纯文本/字符串作为参数

在我的模板中:

{% with  day=forloop.counter|add:"-2" hour=forloop.parentloop.counter|add:"-2" %}
现在,我想使用
{{day}}
{{hour}}
作为参数

例如,我试过:

{% if product.id|is_scheduled:"{{ day }},{{ hour }}" %}...{% endif %}
但这引发了:

异常值:以10为基数的int()的文本无效:“{day}”


你有什么想法吗?

当你在
{%}
中时,你不需要
{{}
。只需直接在该标记中使用名称,并使用字符串concat模板语法
add

如果
day
hour
是字符串,则在将字符串具体化之前,需要将类型转换为字符串:

{% with day|stringformat:"s" as sday hour|stringformat:"s" as shour %}
    {% with sday|add:","|add:shour as arg %}
        {% if product.id|is_scheduled:arg %}...{% endif %}
    {% endwith %}
{% endwith %}

不幸的是,这不起作用。我不知道为什么。天和小时可能是整数。它返回:异常值:“int”对象没有属性“split”,例如,如果我打印dayhour变量,它将打印0而不是“0,2”。这似乎是一个不错的方法,但可能存在一些语法问题。u'with'收到一个无效的标记:u'hour | stringformat:“s”'我正在试图找出错误的原因。好的,我已经用({%with day=forloop.counter | add:“-2”| stringformat:“s”hour=forloop.parentloop.counter | add:“-2”| stringformat:“s”hours=forloop.counter |添加:“-2”|天_到_小时%}),它可以工作。谢谢