绕过 AJAX 调用的 SESSION_SAVE_ON_EVERY_REQUEST 或更好的解决方案
我有一个私人网站,所有页面都需要登录。当用户去编辑记录时,我不想锁定该记录。我想保留该记录以供其他人使用。我设计了一个系统,使用 AJAX 调用 django,使用 dajax/dajaxice 来获取记录的最新编辑人员和日期时间。如果最近的编辑不是由当前用户进行的,则会出现一个警告框,通知用户自打开记录以来,其他人已进行了编辑,并且他们应该刷新页面以获取最新版本的数据。
这一切都很好,并且非常适合我们的情况。我有一个会话超时,超时时会将用户发送到登录提示。如果用户保持页面打开并离开计算机,我们希望敏感数据受到保护。这也运行得很好。
我的问题是,当进行 AJAX 调用来检查数据版本、查看是否更改时,它还会保存会话,因此无论他们在页面上无人值守多久,会话都不会超时。
视图中有没有办法绕过 SESSION_SAVE_ON_EVERY_REQUEST,以便只有此请求不会触发保存。我知道我可以在每个其他视图中手动保存会话,但这似乎是错误的做法。我想我也许能够编写中间件来检查请求的视图,并且仅在不是此视图时保存会话,但我也不确定这是否是最佳解决方案。
有什么建议吗?
提前致谢。
I have a private site that requires login for all pages. When a user goes to edit a record, I don't want to lock the record. I want to keep the record available for others. I devised a system using AJAX calls to django using dajax/dajaxice to get the most recent person and datetime of editing for the record. If the most recent edit was not made by the current user, an alert box notifies the user that another person has made an edit since they've opened the record and they should refresh the page to get the most recent version of the data.
This is all well and good, and works perfectly for our situation. I have a session timing out which, when timed out will send the user to a login prompt. In case the user leaves the page open and leaves the computer, we want the sensitive data protected. This is also working perfectly.
My problem is that when the AJAX call is made to check the version of the data, to see if it is changed, it also saves the session, so the session will never time out no matter how long they are at the page unattended.
Is there a way in a view to bypass the SESSION_SAVE_ON_EVERY_REQUEST, so that only this request does not trigger a save. I know I can manually save the session in every OTHER view, but that seems like the wrong thing to do. I suppose I may be able to write middleware that checks the view requested and only saves the session if it is not this view, but I'm not sure that's the best solution either.
Any suggestions?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我非常确定这会起作用。我将 CustomeMiddleWare 添加到 settings.py MIDDLEWARE_CLASSES 中。另外,在 settings.py 中关闭每个请求的会话保存。
现在的问题是,使用 Dajaxice 时,模块和名称会变成 dajaxice_request。现在我不知道如何获取所请求的实际方法。尝试了很多事情,但一直没能做到。不想破解 DajaxiceRequest 代码,但可能不得不这样做。
解决方案:
针对这一方法切换到 jQuery ajax。创建了一个新的 urls.py 条目来进行单个调用来获取记录的版本。这使得 process_view 中的 view_func 可用,因此我只能将会话保存在除此之外调用的视图上。呜呼!我仍在使用 Dajaxice 来实现所有其他 AJAX 方法。
I am pretty certain this will work. I added CustomeMiddleWare to settings.py MIDDLEWARE_CLASSES. Also, turned off session saving on every request in settings.py.
Now the problem is that using Dajaxice, the module and name come over as dajaxice_request. Now I don't know how to get the actual method requested. Tried a bunch of things, but haven't been able to. Don't want to hack the DajaxiceRequest code, but may have to.
SOLUTION:
Switched to jQuery ajax for this one method. Created a new urls.py entry to make the single call to get the version of the record. This makes the view_func in process_view available, so I can only save the session on views called other than this one. Woo Hoo! I am still using Dajaxice for all other AJAX methodology.