Skip to content

Instantly share code, notes, and snippets.

@gaker
Created October 15, 2013 15:47
Show Gist options
  • Save gaker/6993718 to your computer and use it in GitHub Desktop.
Save gaker/6993718 to your computer and use it in GitHub Desktop.
from django import template
from django.conf import settings
from os.path import join, getmtime
register = template.Library()
@register.simple_tag
def static_asset(filename):
"""
{% load static_asset %}
<link rel="stylesheet" href="{% static_asset "css/main.css" %}" type="text/css">
Renders a link like:
<link rel="stylesheet" href="/media/css/main.css?v=12921928" type="text/css">
"""
try:
filemtime = str(getmtime(join(settings.STATIC_ROOT, filename)))[:8]
except:
filemtime = 0
return '%s%s?v=%s' % (settings.STATIC_URL, filename, filemtime)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment