[Answered ]-Add static ressource to template filter function

1👍

Use static function like this:

# Older Django <3.0 (also deprecated in 2.0):
from django.contrib.staticfiles.templatetags.staticfiles import static

# Django 3.0+
from django.templatetags.static import static

iconUrl = static('images/x-square.svg')

Your code will be:

@register.filter(name = "format_")
def format_(field):
    if field.widget_type == "clearablefile":
        iconUrl = static('images/x-square.svg')
        f = f"""{field}<button class="btn btn-danger"><img src="{iconUrl}"></button>"""
        return mark_safe(f)
    return field

Leave a comment