Django - empty session data in ajax requests

I have an ajax view where I want to set a session variable as follows:

def upload(request, *args, **kwargs): request.session['test'] = 'test' request.session.modified = True print request.session.items() 

I have another normal view something like this:

 def advertise(request): print request.session.items() 

I get these two dicts printed in the shell:

 [('test', 'test')] [('_auth_user_backend', 'django.contrib.auth.backends.ModelBackend'), ('_auth_user_id', 26L)] 

Why doesn't the session data that I set in the ajax view go to my regular views? If I set up session data in normal mode, everything works fine, but it seems that ajax requests contain empty session data? Has anyone done anything similar before? Any suggestions are welcome. Thank you

+4
source share
1 answer

I had the same problem today. Although I don't think the OP is still waiting for an answer in 3 months :-), this may help others.

I sent Ajax requests like this ...

 $.ajax({url: ' http://localhost:8000/testgame/getTime/ ', async: false, dataType: 'text', success: function(text) { time = new Date(text); }, error: function(http, message, exc) { time = new Date(); }}); 

... and access to the application in Firefox:

 http://127.0.0.1:8000/game/config/ 

And the problem is that localhost and 127.0.0.1 in this case do not match!

+5
source

Source: https://habr.com/ru/post/1308037/


All Articles