Django - ajax 请求中的空会话数据

发布于 2024-08-30 11:14:24 字数 605 浏览 2 评论 0原文

我有一个 ajax 视图,我想在其中设置一个会话变量,如下所示:

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

我有另一个普通视图,如下所示:

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

我将这两个字典打印到 shell:

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

为什么我在 ajax 视图中设置的会话数据没有传递到我的常规视图?如果我在常规视图中设置会话数据,一切正常,但 ajax 请求似乎包含空会话数据?以前有人处理过类似的事情吗?非常感谢任何建议。谢谢。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把时间冻结 2024-09-06 11:14:25

我今天也遇到了同样的问题。虽然我不认为 OP 3 个月后仍在等待答案:-),但这可能对其他人有帮助。

我正在发送这样的 Ajax 请求......
<代码>

$.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(); 
    }}); 

...并像这样在 Firefox 中访问应用程序:
<代码>

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

问题是在这种情况下 localhost 和 127.0.0.1 不一样!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文