Search code examples
pythondjangoxmlhttprequesthttpresponse

Assigning values to Django template variables in javascript


<script>
{{ somedjangovariable }}=2;
</script>

In a html page can we do something like this:


Solution

  • If you use

    <script>
    {{ somedjangovariable }}=2;
    </script>
    

    in a Django template, your tag will be filled by the python value of somedjangovariable, computed server-side.

    It can work, but it is quite a strange pattern to use a server-side value to name a javascript variable.