代码之家  ›  专栏  ›  技术社区  ›  Lutaaya Huzaifah Idris

在Django模板中声明自定义标记过滤器

  •  0
  • Lutaaya Huzaifah Idris  · 技术社区  · 3 年前

    我试图声明一个带有2个参数的自定义函数,但它没有按预期工作, 我收到了这个错误:

    Invalid block tag on line 4: 'original|get_customer_by_status:'CONFIRMATION_PENDING'', expected 'endblock'. Did you forget to register or load this tag?
    

    这是我的模板代码:

    {% original|get_customer_by_status:'CONFIRMATION_PENDING' as customer_pending_loan %}
    

    然后我的自定义标签过滤器:

    @register.filter
    def get_customer_by_status(queryset, status):
        print(queryset)
        print(status)
    
    0 回复  |  直到 3 年前
        1
  •  1
  •   AKS    3 年前

    你可以用 with 模板标签:

    {% with customer_pending_loan=original|get_customer_by_status:'CONFIRMATION_PENDING' %}
        your code here where you need to use customer_pending_load variable
    {% endwith %}