11

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 filter in a django template? is almost there, but i dont know how to send my two field names as a string.

my template:

    <th>{{ item.cost_per_month|remaining_cost:item.install_date + ',' + item.contract_length }}</th>

the above didnt work

my template tags:

@register.filter('contract_remainder')
def contract_remainder(install_date, contract_term):
    months = 0
    now = datetime.now().date()
    end_date = install_date + relativedelta(years=contract_term)

    while True:
        mdays = monthrange(now.year, now.month)[1]
        now += timedelta(days=mdays)
        if now <= end_date:
            months += 1
        else:
            break
    return months    

@register.filter('remaining_cost')
def remaining_cost(cost_per_month, remainder_vars):
    dates = remainder_vars.split(',')
    cost = contract_remainder(dates[0], dates[1]) * cost_per_month
    return cost  
Community
  • 1
  • 1
AlexW
  • 2,843
  • 12
  • 74
  • 156

1 Answers1

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 #}

<th>{% remaining_cost item.cost_per_month item.install_date item.comtract_length %}</th>

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 
Munim Munna
  • 17,178
  • 6
  • 29
  • 58
Daniel Schwarze
  • 346
  • 4
  • 10
  • wasnt aware of simple tag! however im getting an error when i use simple tag... " Invalid block tag on line 206: 'remaining_cost', expected 'elif', 'else' or 'endif'. Did you forget to register or load this tag? 206 {% remaining_cost item.cost_per_month item.install_date item.comtract_length %} – AlexW Aug 19 '16 at 11:42
  • Sorry I forgot. You have to put {% load %} – Daniel Schwarze Aug 19 '16 at 12:36
  • At the top of your template – Daniel Schwarze Aug 19 '16 at 12:37
  • hi, yup that already was already added, i have a few other filters loaded on the same .py – AlexW Aug 19 '16 at 14:40
  • I'm not able to point at the problem. could you update your code in your question so I can see what it looks like now ? – Daniel Schwarze Aug 20 '16 at 06:18
  • 1
    you should load the name of python file containing your filters without the `.py` extension. For example {% load %} – thierno Nov 21 '19 at 21:13
  • I got error as well when I have tried to implement but @thierno comment was helpful. The above example is a bit ambiguous what you should load. So if you have a file with multiple template tags you should load the file name instead of the name of the tag({% load my_filters_file %}) – Gabor Mar 18 '22 at 11:56