[Fixed]-In Django, is it Bad to Import and use Template Tags outside a Template?

1👍

No it shouldn’t cause problems.

You can use the function as a regular python function. The @register.filter is a decorator that decorates the function as a filter while register = template.Library() makes it usable as a template tag.

However, it can also be called using the right signature in plain python code.

So you can do:

from django.contrib.humanize.templatetags.humanize import naturaltime
from datetime import datetime as dt

my_human_time = naturaltime(dt.now())
print(my_human_time)
# 'now'

Leave a comment