holyredbeard
holyredbeard

Reputation: 21288

'custom_filters' is not a valid tag library

I'm trying to create custom filters, and I've followed the steps in the Django documention. However, when I load the template that loads the filters the following error is thrown:

'custom_filters' is not a valid tag library

...which refers to the line below in the template:

1   {% extends 'shared/base.html' %}
2   {% load custom_filters %} <--- the error
3   
4   {% block title %}
5       Showing project {{project}}
6   {% endblock %}

The file structure:

project/
    ...
    ...
    models.py
    views.py
    templates/
    templatetags/
        __init__.py
        custom_filters.py

custom_filters.py:

from django import template
register = template.Library()

@register.filter(name='ownership')
def ownership(project, user):
    return project.added_by_user == user

So, by some reason Django can't find the custom_filters file as it seems, even though I have done everything as one should (as far as I know).

What am I doing wrong?

NOTE: Of course I've tried to restart the server.

Upvotes: 1

Views: 3861

Answers (2)

AmiNadimi
AmiNadimi

Reputation: 5755

If your App name is MyApp and your tag folder name is templatetags then in settings.py you should have :

INSTALLED_APPS = [
'MyApp',
'MyApp.templatetags'

]

Both your app and your tag folder which is under your app package Django Project are needed there.

-> MyApp
    ---> models.py
    ---> views.py
    ---> templatetags
      -----> __init__.py
      -----> app_filters.py

Upvotes: 1

catherine
catherine

Reputation: 22808

Template tags folder must be beside of templates folder, views.py, models.py, ...

 //Don't forget also to put __init__.py outside the templatetags,

 @register.simple_tag
 def ownership(project, user):
     return project.added_by_user == user

Upvotes: 1

Related Questions