django: Invalid filter
Asked Answered
L

4

35

I got an article app and trying to make a custom filter, I have a directory called templatetags in article app, and a tags.py within that directory, here is the directory structure.

-manage.py(f)
-settings.py(f)
-articles(d)
 - templatetags(d)
  - tags.py(f)

On the templates, the articles have its own directory, all article templates extend from a base.html template, here is the template structure.

-base.html(f)
-articles(d)
 -index.html(f)

I load the tags in base.html {% load tags %} and use the custom filter in index.html and got the invalid filter error.

tags.py

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

register = template.Library()

@register.filter
@stringfilter
def space2Dash(s):
    return s.replace(' ', '_');

I just can't figure out what I did wrong.

edit: I changed the filter name to abcfilter.py and I have the article app loaded in my settings.py

articles/index.html

 {% load abcfilter %}
 {{ "foo bar"|space2dash }}

the error:

Request Method: GET
Request URL:    http://localhost:8080/articles/
Django Version: 1.2.5
Exception Type: TemplateSyntaxError
Exception Value:    
Invalid filter: 'space2dash'
Exception Location: ***/lib/python2.7/django/template/__init__.py in find_filter, line 363
Python Executable:  /usr/local/bin/python
Python Version: 2.7.1
Server time:    Sun, 10 Apr 2011 07:55:54 -0500
Lorraine answered 9/4, 2011 at 11:21 Comment(0)
C
23

First off remove the semicolon after your replace.

Do you have a file called __init__.py (this is suppose to have 2 underscores before and after init, hard to format in editor.) under the templatetags directory?

Here is a good page with lots of info if you haven't looked yet.

http://docs.djangoproject.com/en/dev/howto/custom-template-tags/

Clinton answered 9/4, 2011 at 11:29 Comment(5)
Thanks for the answer, after remove the smeicolon, the error still there.(fyi, i have a empty init.py in templatetags directory and I have read the doc pages couple times, but I don't know what I did wrong)Lorraine
Try to rename the file from tags to something else. Maybe tags is a reserved word. Do you have the articles app registered in your settings file? Can you post the exact error and the HTML that you are using to call the tag?Clinton
I have change tags.py to abcfilter.py and check the above for the error and html in the templateLorraine
Not sure if it is a typo when transfering to SO, but {{ "foo bar"|space2dash %} should be {{ "foo bar"|space2dash }} also your method is called space2Dash but you are referencing space2dash (all lowercase).Clinton
Thanks Ken, I think I'm going to order a hat from amazon and eat it(the problem turn out to be I got a space2Dash, the capital D :)Lorraine
C
39

Just for reference, I solved the problem by moving

{% load ... %}

from the base template to the concrete template. See also this post https://mcmap.net/q/428790/-django-problems-while-loading-custom-filters-in-the-base-template-file-while-using-template-inheritance

Crow answered 11/7, 2017 at 9:1 Comment(3)
works for me. Putting it in base template seems doesn't work at allBenford
I was having this same issue. Does anyone know why this happens?Athanasius
load 3 dots? or load what?Lucre
C
23

First off remove the semicolon after your replace.

Do you have a file called __init__.py (this is suppose to have 2 underscores before and after init, hard to format in editor.) under the templatetags directory?

Here is a good page with lots of info if you haven't looked yet.

http://docs.djangoproject.com/en/dev/howto/custom-template-tags/

Clinton answered 9/4, 2011 at 11:29 Comment(5)
Thanks for the answer, after remove the smeicolon, the error still there.(fyi, i have a empty init.py in templatetags directory and I have read the doc pages couple times, but I don't know what I did wrong)Lorraine
Try to rename the file from tags to something else. Maybe tags is a reserved word. Do you have the articles app registered in your settings file? Can you post the exact error and the HTML that you are using to call the tag?Clinton
I have change tags.py to abcfilter.py and check the above for the error and html in the templateLorraine
Not sure if it is a typo when transfering to SO, but {{ "foo bar"|space2dash %} should be {{ "foo bar"|space2dash }} also your method is called space2Dash but you are referencing space2dash (all lowercase).Clinton
Thanks Ken, I think I'm going to order a hat from amazon and eat it(the problem turn out to be I got a space2Dash, the capital D :)Lorraine
D
22

I was nearly going nuts with this problem and none of the above answers helped.

If you have several apps, make sure that the file names containing your custom tags/filters are unique, prefereablyapp_name_filters.py. Otherwise Django will only load the custom filters from the app it finds matching first!

Dowel answered 13/10, 2014 at 13:13 Comment(2)
Thanks man! You triggered me to check it... My deployment routine doesn't delete old files on server. So it was working locally, and in testing, but not on server. I was lost. You saved me!Calendre
Somehow even this doesn't work for me (and I'm only talking of the local environment). :( Django can be super weird sometimes.Creativity
Q
3

To avoid loading the module in each template using {% load MODULE_NAME %}, you can add it as a 'builtin' in settings.py:

TEMPLATES = [
    {
        'OPTIONS': {
            ...
            ,
            'builtins': [
                ...
                'APP_NAME.templatetags.MODULE_NAME',
                ]
        },
    },
]
Quadrangle answered 1/1, 2021 at 17:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.