different values of a django session variable in different tabs

ⅰ亾dé卋堺 提交于 2019-12-08 01:10:57

问题


Consider the following scenario:

  1. User searches for something and a list (request.session['List']) is created
  2. User can filter this list via an ajax call
  3. Now the user opens up a new tab, does another search, so now the session variable List is set to the new list for the other search
  4. User goes back to the first tab and filters the results again. This time, the filter results come from the new list in the other tab as the session variable has changed

Is there a way to set different values for a session variable for different tabs? or any other solution for this problem?


回答1:


There is no easy way to do this and it's not Django specific. Check this question:

How to differ sessions in browser-tabs?.

Session based on cookie will not certainly work as cookie is common between tabs for a specific site. Solutions based on URLs with session or local storage have their own issues and in general this is not a good idea because it adds a complexity that is not required in most cases.

In your case, why don't you store the list as JavaScript data or local storage? In that case each tab has its own data.




回答2:


The server application identifies your requests and session by your session ID, this means that it does not know about tabs and such. In fact, if you give me your session ID, I will get the same list(see Session Hijacking not to get into such troubles).

That being said, if you really want to do that, you could play around with saving user-agent into your session, or make use of request.is_ajax()

You could have session['List'] = ... and session['List_ajax'] = ...` Then you whould do: return session['List_ajax'] if request.is_ajax() else sessoion['List']



来源:https://stackoverflow.com/questions/17103280/different-values-of-a-django-session-variable-in-different-tabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!