Mr Panda
Mr Panda

Reputation: 101

custom template tag with multiple arguments

I want to format a date in the future based on a time delta:

from django import template
from datetime import datetime, timedelta, time


register = template.Library()

@register.simple_tag
def tomorrow(format):
    tommorow = datetime.now() + timedelta(days=1)
    return tommorow.strftime(format)

def dayfuture(dday, format):
    dayfuture = datetime.now() + timedelta(days=dday)
    return dayfuture.strftime(format)

This works:

{% tomorrow "%A, %d %b, %Y" %}

But I've had no luck with dayfuture.

Also, is it possible to have multiple custom template tags in the same file. I've had no luck registering a second one.

I'm using django 1.11 pythone 3.4

Upvotes: 1

Views: 202

Answers (1)

Arpit Solanki
Arpit Solanki

Reputation: 9921

This does not work because you did not register it. It is possible to have multiple template tags inside a single file.

def dayfuture(dday, format):
    dayfuture = datetime.now() + timedelta(days=dday)
    return dayfuture.strftime(format)

You have to put the decorator on it to register it

@register.simple_tag
def dayfuture(dday, format):
    dayfuture = datetime.now() + timedelta(days=dday)
    return dayfuture.strftime(format)

Upvotes: 1

Related Questions