简体   繁体   English

在Django中保存动态页面

[英]Save dynamic page in Django

A page in my django app lets the user zoom and drag an image. django应用程序中的页面允许用户缩放和拖动图像。 I'd like to save the zoom level and the image position so that the next time I view the page, the image is back to the state of my last visit. 我想保存缩放级别和图像位置,以便下次查看该页面时,图像恢复到上次访问的状态。

The django sessions docs clearly show how to set session info, but only once you're in the Python code. django会话文档清楚地显示了如何设置会话信息,但是只有在您使用Python代码时才如此。 The detail I'm unclear on is how to execute "session updating" code whenever I navigate away from my page. 我不清楚的细节是每当我离开页面时如何执行“会话更新”代码。

Is is possible to keep my saved session synced with javascript variables in a django template? 是否可以让我保存的会话与Django模板中的javascript变量同步?

You need your web page to send that information to your server so it can update it in the session. 您需要网页来将该信息发送到服务器,以便它可以在会话中更新它。 The way session works in django is it only sends a session id to your web page(usually through cookie) and then that cookie is sent back with every request your web page does(cookie is part of HTTP protocol and managed by the web browser). 会话在django中的工作方式是,它仅将会话ID发送到您的网页(通常通过cookie),然后该cookie随您的网页执行的每个请求一起发送回去(cookie是HTTP协议的一部分,由网络浏览器管理) 。

The template code is run on the server to output html and javascript but none of this is actually run on the server and the template is not a live connection to your server. 模板代码在服务器上运行以输出html和javascript,但是这些代码实际上都不在服务器上运行,并且模板不是与服务器的实时连接。 Merely setting variable in javascript will not get them back to the server. 仅在javascript中设置变量不会使它们返回服务器。

You could however send the saved variable in a request to your server using an ajax request for example so your server puts them in the session for later use. 但是,您可以使用ajax请求将请求中的已保存变量发送到服务器,例如,以便服务器将其放入会话中以供以后使用。

jQuery provides an unload event to be notified when the user goes away. jQuery提供了一个卸载事件,当用户离开时会得到通知。 You could bind to this event to do your update. 您可以绑定到此事件以进行更新。

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

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