Consider the following scenario:
- User searches for something and a list (request.session['List']) is created
- User can filter this list via an ajax call
- 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
- 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?
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.
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'] = ...
andsession
['List_ajax'] = ...` Then you whould do: return session['List_ajax'] if request.is_ajax() else sessoion['List']