Django cannot find my own template filter

I am trying to create a custom filter in my Django application. I created the templatetags folder in my application, added the __init__.py file and the filters.py file with my filters, for example:

 import os from django import template from django.template.defaultfilters import stringfilter register = template.Library() @register.filter @stringfilter def filename(pathname): """ Returns the file name, without folder """ return os.path.basename(pathname) 

When I try to access a filter from a template, I get the error message "Invalid filter" filename '"

This question is asked very often, so I already checked the following:

  • __init__.py correctly named
  • After opening the shell with manage.py shell I can import my filter as follows: import app.templatetags
  • I can also use the filter from the Django shell correctly
  • I restarted the Django development server.
  • I set a breakpoint when calling template.library() - the debugger does not stop there.

Here is the template (I deleted parts of it, but the error persists in this small version)

 <form enctype="multipart/form-data" action="{% url lab.views.new.artefact_drop_web tempfile_id=tempfile.id %}" method="post"> {% csrf_token %} <h3>File:</h3> <p>{{ tempfile.file.name|filename }}</p> <input type="submit" value="Add" /> </form> 
+4
source share
1 answer

You need to use {% load name_of_file_tags_are_in %} inside your template.

https://docs.djangoproject.com/en/1.5/howto/custom-template-tags/

+10
source

All Articles