1
You can register a filter that you call on all static file paths, and pass the template static file path to your context.
First make an appropriate filter:
from django.template import Library
from urlparse import urljoin
register = Library()
from django.conf import settings
@register.filter
def make_static(relative_url, template_dir):
base = urljoin(settings.STATIC_URL, template_dir)
return urljoin(base, relative_url)
Now when rendering your template add a reference to where the template static files are based from:
from django.template import Context
from django.template.loader import get_template
template = get_template('template1/index.html')
context = Context({'template_dir': 'template1/'})
template.render(context)
In your actual templates use the filter like so:
<img src="{{'imgs/some_image.jpg'|make_static:template_dir}}">
This will really only be useful if each of your templates inherit from some base template that uses these generic paths, but each template needs a different image or something to look the way you’d like.
Source:stackexchange.com