Django Rest Framework React.js会话无法正常工作 [英] Django rest framework Reactjs sessions not working

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

问题描述

因此,我已经将Django rest框架设置为电子商务网站的后端API.该网站是通过React前端显示的,而django后端不提供该服务.

So I have set up Django rest framework as a backend API for an e-commerce website. The website is displayed through a React frontend, which is not served by the django backend.

我目前正在本地开发服务器上运行Django后端和React前端( http://127.0.0.1:8000 http://127.0.0.1:3000 ).将来,它们可能会位于不同的域中.

I am currently running both the Django backend and the React frontend from their local development servers (http://127.0.0.1:8000 and http://127.0.0.1:3000 respectively). In the future they will be on separate domains, probably.

当我在一个视图中设置一个会话,然后在另一个视图中读取内容时,如果我只键入用于创建并直接读取到浏览器中的URL(仅出于测试目的),则此方法有效.但是,当我通过前端访问后端时,无法再访问会话,或者似乎无法存储会话.将会发生的是,当我尝试访问在上一个视图中设置的数据时,出现了KeyError.

When I set a session in a view, and read the content in another, this works if I just type in the urls for creating and reading directly into my browser (just for testing purposes). But when I access the backend through my frontend, sessions can not be accessed anymore, or don't seem stored. What will happen is that I get a KeyError when trying to access the data that I set in a previous view.

我想这与我前一段时间读过的东西有关,但是我发现很难找到有关如何使用它的正确信息.这是否与具有会话ID的cookie无关,而该cookie对前端不可用,而仅对后端本身可用?

I guess this has to do with something I have read about some time ago, but I find it hard to find the correct information on how to work with this. Does this have to do with the cookie with the session id not being available to the frontend, but only to the backend itself?

主要问题:我想知道如何使用上述设置进行会议,以保持购物车.

Main question: I would like to know how I can work with sessions, using the above settup, for keeping a shopping cart.

我的后端代码,以防万一有人怀疑:

from django.http import HttpResponse


def cart_add(request, product_id, update, quantity):
    request.session['one'] = 'created through "cart_add" view'
    return HttpResponse("Created a session - cart_add")


def create(request):
    request.session['one'] = 'created through "read" view'
    return HttpResponse("Created a session - create")


def read(request):
    print(request.session['one'])

我删除了一些不必要的代码.

I have removed some unnecessary code.

  • 使用ajax调用(axios)从React前端调用cart_add视图.
  • 我通过直接在浏览器中键入其URL来调用创建视图和读取视图.(所有这些都是出于测试目的,只需在开始编写实际代码之前确保会话正常工作即可.)

推荐答案

我在另一个stackoverflow问题中找到了解决方案.这是指向它的链接.

I've found a solution in another stackoverflow question. This is the link to it.

通过将以下内容添加到我的axios请求中,代码可以成功运行:

By adding the following to my axios request, the code works successfully:

axios.get('some api url', {withCredentials: true});

因此,我对会话ID不可用于前端的cookie的假设似乎是错误的.

So it seems my assumption about the cookie with the session id not being available to the frontend is incorrect.

我还发现,通过在Chrome中打开网页,然后打开开发人员工具>转到应用程序"标签>单击Cookie,可以看到Cookie.

I also found out that I could see the cookie by opening the web page in Chrome, then opening the developer tools > going to 'application' tab > click on cookies.

此处列出了所有可用的cookie,并显示了一个sessionid cookie.

Here all the available cookies are listed, and also a sessionid cookie is shown.

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

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