Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save carymrobbins/8428159 to your computer and use it in GitHub Desktop.
Save carymrobbins/8428159 to your computer and use it in GitHub Desktop.
Inject JavaScript variables into a Django {% url %} template tag.
from django.template import Context, Template
print Template("""<script type="text/javascript">
var foo = 1,
bar = 2,
url = '{% url view_name obj "'+foo+'" "'+bar+'" %}';
</script>""").render(Context(dict(obj='something')))
# Output:
<script type="text/javascript">
var foo = 1,
bar = 2,
url = '/path/to/something/'+foo+'/'+bar+'/';
</script>
@Amamgbu
Copy link

Amamgbu commented Feb 24, 2020

Thank you for this. This was really helpful

@raj-pulapakura
Copy link

Incredible. Exactly what I needed! Thanks!

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