Get Error: jinja2.exception.TemplateAssertionError: no filter named 'basename'

10,717

Solution 1

From what I see in Jinja2 official documentation there is no basename filter.
See Jinja2 - List of Builtin Filters.

EDIT:
You can write yor own basename filter, for example:

def basename(text):
    return text.split('\\')[-1]

app.add_template_filter(basename)

Solution 2

I had a similar error in a stand-alone program that uses Jinja2. In my case I didn't have an app, so I couldn't use app.add_template_filter. I had an Environment instance which has a filters dict. I added to that dict and the filtering went fine. If 'e' is the environment, I did something like this:

e.filters['basename'] = basename

Assuming a basename function in the previous answer. With an app, app.jinja_env exposes the environment, so that becomes:

app.jinja_env.filters['basename'] = basename
Share:
10,717
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    I get this type of error while creating template file. I have simple use this type of syntax:

    {{ "hello\how\are" | basename}}
    

    Please help me solve it.

    Thanks