Django - How to use my custom filters inside any included template?

638 views Asked by At

I can't make use of my filters on my included child view, the filter works when is written directly, but I need the child view as inclusion...

Here is the filter:

from django import template
from django.template.defaultfilters import stringfilter

register = template.Library()

@register.filter
def lower(value):
    return value.lower()

Here is how I call my template:

{% load app_filters %}
{% include 'view.template.html' %}

view.template.html

<h1>{{ 'HELLO WORLD!' | lower }}</h1>

But the thing is that it doesn't work been included, unless I had to add the {% load app_filters %} inside view.template.html but I need this template for my angular app too, so I can't write this line in the template. It is a shared template for my angular app and separated view for no-angular template in Django.

How could I inject my custom filters to the included view without modifying the view? It is possible to pass as {% include 'view.template.html' with app_filters=app_filters %}? It is only the idea.

1

There are 1 answers

0
Emisael Carrera On

I'm solving at the moment with:

<div ng-hide="true">
    {% load app_filters %}
</div>

In view.template.html, so it won't affect the template in angular, and lets load the django's filters as expected to be.