React Django REST框架会话无法持久/正常工作 [英] React Django REST framework session is not persisting/working

查看:103
本文介绍了React Django REST框架会话无法持久/正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个以Django Rest Framework为后端,React为前端的项目.当我最初在某个函数/视图中设置会话变量,然后当我尝试通过axios调用访问另一个视图时,如果尝试访问之前创建的会话变量,则在该视图中设置KeyError.会话似乎没有存储.

I'm working on a project with Django Rest Framework as back-end and React as front-end. When I set a session variable initially in some function/view and later when I try to access the different view through axios call and in that view if I try to access session variable which i created previously, I get KeyError. Session doesn't seem stored.

我用谷歌搜索我遇到的类似问题.

I googled I got the similar issue which I'm facing.

Django rest框架Reactjs会话不起作用

我遵循了此过程,在axios调用中添加了 {withCredentials:true} .现在我得到了不同的错误.现在,该问题无法访问后端.我在" http://127.0.0.1:8000/url/"(来自来源" http://localhost:3000 "已被CORS政策阻止)

I followed the process by adding { withCredentials: true } in axios call. Now i'm getting different error. Now the issue is not able to access the backend. I get an error saying Access to XMLHttpRequest at 'http://127.0.0.1:8000/url/' from origin 'http://localhost:3000' has been blocked by CORS policy

我再次搜索了我遇到的问题,发现我必须在django settings.py中添加CORS_ORIGIN_WHITELIST.

Again I googled the issue which i'm getting and found that I've to add CORS_ORIGIN_WHITELIST in the django settings.py

我关注了以下帖子

Django Python rest框架,在chrome中请求的资源上不存在"Access-Control-Allow-Origin"标头,在Firefox中有效

我已经这样添加了CORS_ORIGIN_WHITELIST

I have added CORS_ORIGIN_WHITELIST like this

CORS_ORIGIN_WHITELIST = [ ' http://localhost:3000 ', ' http://127.0.0.1:3000 ' ]

CORS_ORIGIN_WHITELIST = [ 'http://localhost:3000', 'http://127.0.0.1:3000' ]

尽管如此,我仍然面临着同样的问题.我不知道怎么了.谁能在这个问题上帮助我.

Still i'm facing the same issue. I don't know whats going wrong. Can any one please help me on this issue.

谢谢.

推荐答案

最后,经过大量研究,我找到了解决方案.

Finally after so much of research I found a solution for this.

在我们要导入axios进行呼叫的文件中,将默认标头设置为您的导入下方 axios.defaults.withCredentials = true;

In the file where we are importing axios to make the call, set the default header below your import axios.defaults.withCredentials = true;

示例:

import axios from "axios";

axios.defaults.withCredentials = true;

axios.get("url")
.then(response => { 
   console.log(response) 
})
.catch(error => {
    console.log(error);
});

完成此操作后,转到您的settings.py文件并添加以下配置

once this is done go to your settings.py file and add the below configuration

CORS_ORIGIN_ALLOW_ALL =真

CORS_ALLOW_CREDENTIALS =真

因此,在此之后,如果您设置会话变量并以后在任何视图中访问它,您将能够获取先前存储的值.

so after this if you set a session variable and access it later in any view, you would be able to get the value which you had previously stored.

此解决方案对我有用.希望如果有人遇到同样的问题,它也将对他们有用. :)

This solution worked for me. Hopefully if anyone has the same issue, it will work for them too. :)

注意

如果会话未存储在 localhost:3000 下,请确保您正在通过 127.0.0.1:3000 访问应用程序.如果您通过 localhost 访问该应用程序,则cookie将存储在 127.0.0.1 下,因此将URL从 localhost:3000 更改为 127.0.0.1:3000 将解决您的问题.

If session is not storing under localhost:3000 then make sure that you're accessing your application through 127.0.0.1:3000. If you access the application through localhost then the cookie will be stored under 127.0.0.1, so by changing the URL from localhost:3000 to 127.0.0.1:3000 will solve your problem.

这篇关于React Django REST框架会话无法持久/正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆