sergzach sergzach - 25 days ago 7
Python Question

How to create Jinja2 custom filters with django_jinja app?

I can't figure out how to access

environment.filters
. 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
render_to_response
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: https://pypi.python.org/pypi/django-jinja/0.8.

Answer

I used this...

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

Hope this helps!