我正在尝试创建一个具有多个参数的过滤器。我发现this link的第一个答案很有用,所以采纳了他的想法。你知道吗
但是,我不知道如何创建包含所有值的字符串变量。这是我的密码:
{% with amount_comments|to_string+","+limit_amount_comments|to_string as args %}
{% if page_nr|page_not_over_amount:args %}
<a href="{% url 'post:detail' topic.id page_nr|increase:1 %}">Next Page</a>
{% endif %}
{% endwith %}
这是一个字符串过滤器:
@register.filter(name='to_string')
def to_string(value):
return str(value)
然后使用以下参数将页面\u not \u over \u amount筛选器:
@register.filter(name='page_not_over_amount')
def page_not_over_amount(page_nr, args):
if args is None:
return False
else:
arg_list = [arg.strip() for arg in args.split(',')]
comment_amount = arg_list[0]
comment_limit = arg_list[1]
if page_nr*comment_limit < comment_amount-comment_limit:
return True
else:
return False
但我有个例外:
Could not parse some characters: amount_comments|to_string|+","+limit_amount_comments||to_string
提前谢谢!你知道吗
我认为更好的方法是编写一个自定义标记。尽管如此,您仍然可以使用添加过滤器生成单个字符串值,该值可以使用
with
标记引用为args
。你知道吗奖金:
清理
page_not_over_amount
使用自定义分配标签
定义一个恒等函数
assign_tuple
为simple custom assignment tag。 此函数具有传递给它的返回参数。你可以更进一步。你知道吗在模板中使用此标记,如下所示:
相关问题 更多 >
编程相关推荐