0

Currently I'm working on a project where I'm using React as frontend and Django as backend. In react i created a login page, where I through axios send files to django, and in this route index i sent all the information from the login page, and define a session variable reqeuset.session['id']=150 in it. But when i call reqeust.session['id'] in a diffrent route, it says that it is type None.

This is the code:

@api_view(['POST'])
def index(request):
     data=request.data.get('data')
     korisnik = Korisnik.objects.filter(korisnicko_ime=data.get('user'),
     if korisnik.exists():
         korisnik_json=serializers.serialize('json',korisnik)
         request.session['id']=150
         print(request.session['id'])
        # if not request.session.session_key:
        #    request.session.create()
         return HttpResponse(korisnik_json)
     else: return HttpResponse(status=404)

 @api_view(['GET']) 
 def korisnik(request,id):
     print(request.session.get('id'))
     korisnik=Korisnik.objects.filter(pk=id)
     korisnik_json=serializers.serialize('json',korisnik)
     return HttpResponse(korisnik_json)

This is the output from python console Image

Also note, I'm using django restframework. Did anyone, have this problem before, any help is appreciated.

  • I believe the session is not persisting between the route changes. Can you share your Axios code? are you using withCredentials in you axios code? – Aashay Amballi Jul 10 '21 at 20:47
  • Here is the axios code https://pastebin.com/F5sTskDg, i don't use withCredentials i tried putting withCredentials=true, but then i get this error https://imgur.com/chDH9qr – Tarik Pašić Jul 10 '21 at 20:52
  • If it solves your problem, please accept it as the answer here and in the original post. – Aashay Amballi Jul 10 '21 at 20:55

1 Answers1

0

I had faced a similar issue. Please check my answer here.

React Django REST framework session is not persisting/working

Dharman
  • 30,962
  • 25
  • 85
  • 135
Aashay Amballi
  • 1,321
  • 3
  • 17
  • 39