简体   繁体   中英

Extract a javascript variable with flask

I am working on a little flask project and in my javascript script I have a variable which works as a counter.

When I receive a POST request I would like my python script to extract this counter variable.

I tried to set a jinja2 variable by doing {%set extractor_var = js_counter%} but it seems to be impossible to use a javascript variable inside a jinja2 template.

Can anyone lead me to another solution?

That doesn't make sense!
Flask/Jinja can't read from javascript vars.
However, (as you said it is a POST request) you could:

Three ways:

  • Passing (dynamically modifying DOM link or form action URL) counter var value to POST request URL, like: /path/postaction?counter=4;
  • If it's a POST request from form you could modify form action (see above) or adding a input hidden to form;
  • Setting a cookie and get it in the next request (I don't like this option);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM