简体   繁体   English

如何使用Python在HTML本地存储中设置数据?

[英]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). 我想使用Python在HTML本地存储中设置数据(我正在使用Flask框架)。 Do I need to use JavaScript? 我需要使用JavaScript吗?

You need to use JavaScript. 你需要使用JavaScript。

Python (server-side) knows nothing about JavaScript (client-side) where localStorage resides. Python(服务器端)对localStorage所在的JavaScript(客户端)一无所知。

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 您在HTML页面中运行了JavaScript JS代码段

  • This JS makes an AJAX request to the server-side Flask view. 这个JS向服务器端Flask视图发出一个AJAX请求。 Try jQuery.getJSON() 试试jQuery.getJSON()

  • Flask view returns JSON data Flask视图返回JSON数据

  • JS code receives the JSON and stores it in the localStorage JS代码接收JSON并将其存储在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. 或者,如果数据量是几个字节,您可以使用cookie将其从服务器端传递到客户端(JavaScript)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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