In a typical view file you have to think about where the execution will
occur.
The Python code in the view will run on the server as part of the page
generation process, the page is shipped out to the user's browser as the
response and will be HTML and Javascript and the Javascript that possibly is
embedded in that page is run by the user's browser. Given that situation
there is no longer any Python code for the Javascript to assign a variable
into.
Your only hope is to generate an AJAX request from the Javascript which will
send a new request to the server as a way to interact back with the Python
code on the server.