根据条件更改模板选项

时间:2017-12-26 10:21:21

标签: python django django-templates

如何根据其他模板标记更改模板标记。

task_count = {1:3,2:0,3:1} ---我手动创建

task_type = {1:rebuild,2:upgrade,3:provisioning} ----来自表格

我想得到ex的任务计数:如果任务id为1则“Rebuild:3”。

我尝试了以下代码,但它无效

{% for task in TaskTypeTag %}
{{task_count.{{task.id}}}}

1 个答案:

答案 0 :(得分:0)

您必须创建自定义过滤器。

@register.filter(name='cut')
def get_count(value, arg):
    task_count = {1 :3, 2: 0, 3: 1}
    return "{}:{}".format(value, task_count[arg])

然后您就可以在模板中使用它了。

{% for task in TaskTypeTag %}
    {{task.name|get_count:task.id}}

有关详情,请点击here

相关问题