sergzach sergzach - 1 year ago 101
Python Question

How to create Jinja2 custom filters with django_jinja app?

I can't figure out how to access

. In standart Jinja2 examples I can see an example of datetimeformat filter:

def datetimeformat(value, format='%H:%M / %d-%m-%Y'):
return value.strftime(format)

Then we can add the filter:

environment.filters['datetimeformat'] = datetimeformat

But I attached django_jinja application and now use a standart
method from django.shortcuts (Jinja's macros, built-in functions work OK). So my views make such responses:

return render_to_response( html_template, result_dict )

I only included Jinja2 app and don't worry about constructing custom Jinja2 response, but I don't know how to access the environment.

What should I change to add a custom template filter with django_jinja application?

Here is a description of the app:

Answer Source

I used this...

jinja2.filters.FILTERS['datetimeformat'] = datetimeformat

Hope this helps!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download