Forked from carymrobbins/django-javascript-variable-interpolation.py
Created
June 14, 2020 14:54
-
-
Save essamalsaloum/82feccc7d9b32679d9d58d20869c746e to your computer and use it in GitHub Desktop.
Inject JavaScript variables into a Django {% url %} template tag.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment