I'm trying to create a custom template filter in my Django app. I've created a templatetags folder under my app, added an __init__.py
file and a filters.py file with my filters, like so:
import os
from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
@register.filter
@stringfilter
def filename(pathname):
""" Returns the file's name, without folder """
return os.path.basename(pathname)
When I try to access the filter from a template, I get the error "Invalid filter 'filename'"
This question has been asked a lot, so I already checked the following:
__init__.py
is properly namedmanage.py shell
I can import my filter like so: import app.templatetags
template.library()
- the debugger doesn't stop there.Here is the template (I've removed parts of it, but the error persists in this small version, as well)
<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>
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With