Ricardo B.
Ricardo B.

Reputation: 351

Is it possible to use a django filter on the result of a templatetag?

I have a template tag which returns datetime objects converted to the user's timezone. To format it, I'd prefer the builtin filter "date" to format it.
Is it possible to use a filter on the result of a template tag?

Upvotes: 3

Views: 1038

Answers (3)

tikej
tikej

Reputation: 322

{% your_tag_name as name %}
{{ name|filter }}

Upvotes: 0

tsouvarev
tsouvarev

Reputation: 101

You can wrap your custom templatetag with 'filter' templatetag:

{% filter date %}
    {% your_custom_tag %}
{% endfilter %}

More information here: https://docs.djangoproject.com/en/dev/ref/templates/builtins/#filter

Upvotes: 10

Brandon Taylor
Brandon Taylor

Reputation: 34593

Yes, if course you can. Filters are just functions that can be imported. Import the filter function you need from django.template.defaultfilters and pass your arguments to it.

Upvotes: 0

Related Questions