2

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?

user_2000
  • 1,103
  • 3
  • 14
  • 26

2 Answers2

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.

Community
  • 1
  • 1
maulik13
  • 3,656
  • 1
  • 25
  • 36
  • Thanks, I'm using django-endless pagination, and when you scroll down, each time a page is loaded, an ajax request is sent to the back end and the results are fetched, dont think the pagination will work if I save it as JS data, any other ideas to make it work? – user_2000 Jun 14 '13 at 11:05
  • Yes if you are loading the page again then JS data won't work. I assumed you were doing only AJAX data loading. Do you have possibility of using local storage then? – maulik13 Jun 14 '13 at 14:44
0

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

Ciprian Tarta
  • 484
  • 5
  • 12
  • thanks, but I didnt completely understand your solution, since I'm essentially creating the same list type (with different content), how can I send a normal request from one tab and an ajax request from another? – user_2000 Jun 14 '13 at 11:11
  • My understanding was that the search would not be an ajax call, only the filtering after the list is returned. If that's not the case, then see bellow – Ciprian Tarta Jun 14 '13 at 12:30