different values of a django session variable in d

2019-05-29 04:39发布

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?

2条回答
手持菜刀,她持情操
2楼-- · 2019-05-29 05:18

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.

查看更多
神经病院院长
3楼-- · 2019-05-29 05:23

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']

查看更多
登录 后发表回答