简体   繁体   中英

How to set data in HTML local storage using Python?

I want to set data in HTML local storage using Python (I am using Flask framework). Do I need to use JavaScript?

You need to use JavaScript.

Python (server-side) knows nothing about JavaScript (client-side) where localStorage resides.

The easiest, however many times not the most efficient, way to approach the problem is

  • You have a JavaScript JS snippet running in your HTML page

  • This JS makes an AJAX request to the server-side Flask view. Try jQuery.getJSON()

  • Flask view returns JSON data

  • JS code receives the JSON and stores it in the localStorage

Alternative, if the amount of data is few bytes, you can pass it from the server-side to the client-side (JavaScript) using cookies.

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