1

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.

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.

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.

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.

My backend code, just in case someone wonders:

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.

  • The cart_add view is called from the React frontend, using an ajax call (axios).
  • The create and the read view I called by typing their urls directly into the browser. (This is all done for testing purposes, just making sure sessions are working before I start to write the real code.)
Rik Schoonbeek
  • 2,501
  • 1
  • 15
  • 30
  • Did you set X-CSRFToken request header before making ajax call? If not check this https://docs.djangoproject.com/en/1.10/ref/csrf/#ajax – Nagesh Dhope Jun 01 '18 at 10:23

2 Answers2

2

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

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

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

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

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.

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

Rik Schoonbeek
  • 2,501
  • 1
  • 15
  • 30
0

I had the same issue, by adding withCredentials in axios call didn't solve my problem in django 2.2.3 and axios 0.19.0.

If the answer here doesn't work for you, then look into the below answer :)

React Django REST framework session is not persisting/working

Aashay Amballi
  • 615
  • 1
  • 7
  • 22