Last active
June 29, 2022 10:50
-
-
Save carymrobbins/8428159 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> |
Incredible. Exactly what I needed! Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you for this. This was really helpful