import os
from django import template
from django.templatetags.static import static
from django.contrib.staticfiles import finders

register = template.Library()

@register.simple_tag
def static_v(path):
    url = static(path)
    absolute_path = finders.find(path)
    if absolute_path and os.path.exists(absolute_path):
        mtime = int(os.path.getmtime(absolute_path))
        return f"{url}?v={mtime}"
    return url
