开发者

Django - Empty session data in ajax requests

开发者 https://www.devze.com 2022-12-28 00:23 出处:网络
I have an ajax view where I want to set a session variable like such: 开发者_开发知识库def upload(request, *args, **kwargs):

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.


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!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号