zmbq
zmbq

Reputation: 39013

Django can't find my custom template filter

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:

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>

Upvotes: 4

Views: 8615

Answers (1)

Ngenator
Ngenator

Reputation: 11259

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/

Upvotes: 13

Related Questions