Skip to content

Instantly share code, notes, and snippets.

@tadeo
Created June 29, 2018 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tadeo/c88ac1f6a482c65fbebe8961c6bc97f7 to your computer and use it in GitHub Desktop.
Save tadeo/c88ac1f6a482c65fbebe8961c6bc97f7 to your computer and use it in GitHub Desktop.
from django import template
from django.conf import settings
register = template.Library()
@register.simple_tag(takes_context=True)
def absolutize_path(context, *args):
relative_url = ''.join(args)
absolute_paths = context.get('absolute_paths', False)
if absolute_paths:
if relative_url.startswith('/'):
relative_url = relative_url[1:]
return '{}/{}'.format(settings.APPS_DIR, relative_url)
else:
return relative_url
@tadeo
Copy link
Author

tadeo commented Jun 29, 2018

Use it in templates as:

{% load absolutize_path %}
...
<script src="{% absolutize_path STATIC_URL 'js/project.js' %}"></script>

if you pass absolute_paths=True in the context the output will be the absolute file system path as /app/app_name/static/js/project.js, else you will get the standard /static/js/project.js

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment