Access Javascript variable in Django Template

2024/2/27 7:50:41

Is there is any way to access javascript variable in Django template tags ?
Can i do something like this ,

<!DOCTYPE html>
<html><body><script>var javascriptvar=0;</script>{% if javascriptvar==0 %}do this{% else %}do this{% endif %}</body>

No, the Django template is compiled server side. It is then sent to the client where their browser executes the JavaScript. Nothing that is changed by the JavaScript executing on the client browser can have an affect on the template. It's too late at that point.

However the JavaScript could do something like make another request from the server for more information. Or you could just pre-compute the value on the server before you send it to the client. If you are more explicit about what you are trying to do we should be able to help.

You can of course use Django templates to set JavaScript variables.

<script>var myVar = '{{ py_var }}';

Related Q&A

Angular 4 unit test for KeyboardEvent

Im trying to write a unit test for a utility Ive written to limit the characters available to an input field. The method takes the keyboard event and determines which event.code has been fired and eit…

Javascript: Scroll to cursor post a paste in contenteditable div

I have a div with contenteditable attribute value set to true. When I paste some text in this contenteditable, I am able to keep the mouse position at the end of the pasted text. When there is a large …

Jade Templates - Dynamically Calling a Mixin

How can I use a string from the json being fed into a Jade template to dynamically load a mixin? Below, the goal is for twoColumn.jade to load the foo and bar mixins.twoColumn.jademixin twoColumns(obj…

Why is apply not already bound to functions in Javascript?

Assume, for the sake of this question, that I want to be able to create a function in Javascript that appends all of the elements of one array to another array. One way to achieve this, if you have acc…

fixing gridview header while scrolling

I had fixed GridView header and while i scroll down headers will be constant but the problem is headers are not in proper format. Like all the column headers are shrinked and wont display under particu…

How to consume the download stream from Axios using StreamSaver.js?

On my server-side, which is built using Spring Boot framework, it returns a stream which looks like this: public ResponseEntity<StreamingResponseBody> downloadFiles(@RequestBody DownloadRequest p…

JavaScript regex to replace a whole word

I have a variable:var str = "@devtest11 @devtest1";I use this way to replace @devtest1 with another string:str.replace(new RegExp(@devtest1, g), "aaaa")However, its result (aaaa1 aa…

IE requiring multiple clicks on submit

I have a form with a file control and a separate button that causes the click event to fire on that file field. The problem is that if you click the button, then the submit button at the bottom of the…

Disadvantages mutating state directly and forceUpdate() vs setState

React docs say Never mutate this.state directly, as calling setState() afterwards may replace the mutation you made. Treat this.state as if it were immutable... But this is a non issue when I dont use …

Is it possible to abort a synchronous XmlHttpRequest?

I have written a JavaScript function that asynchronously calls a web service using XmlHttpRequest. I have been asked to make this function finish its work before the page is rendered.I thought I could …