Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django - Empty session data in ajax requests

I have an ajax view where I want to set a session variable like such:

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 to shell:

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

Why is the session data that I set in the ajax view not passing to my regular views? If I set session data in regular view, everything works as fine, but it seems that ajax requests contain empty session data? Anybody dealt with something like this before? Any suggestions are greatly appreciated. Thanks.

like image 957
ninja123 Avatar asked Apr 26 '10 15:04

ninja123


1 Answers

I was having the same problem today. Although I don't think the OP is still waiting for an answer after 3 months :-), this might help others.

I was sending out 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 accessing the application in Firefox like this:

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

And the problem was that localhost and 127.0.0.1 are not the same in this case!

like image 154
dsetton Avatar answered Oct 04 '22 04:10

dsetton