Django template, send two arguments to template tag?

前端 未结 1 595
再見小時候
再見小時候 2020-12-19 16:02

Can anyone tell me if its possible to send multiple variables from field names to a template tag?

this question How do I add multiple arguments to my custom template

1条回答
  •  青春惊慌失措
    2020-12-19 16:31

    From my point of view it looks easier to use a simple tag instead of a template filter so you can call it without needing to send a string.

    https://docs.djangoproject.com/en/1.10/howto/custom-template-tags/#simple-tags

    Your template would be just:

    {% load remaining_cost %}
    {# Don't forget to load the template tag as above #}
    
    {% remaining_cost item.cost_per_month item.install_date item.comtract_length %}
    

    and the template tag would be:

    @register.simple_tag
    def remaining_cost(cost_per_month, install_date, contract_length):
        cost = contract_remainder(install_date, contract_length) * cost_per_month
        return cost 
    

    0 讨论(0)
提交回复
热议问题