When I log a user in using
django.contrib.auth.login function,
session_key is sometimes not set in the
request of the view.
Code in
views.py:
from django.contrib import auth
from django.contrib.auth.models import User
from django.http import HttpResponse
import json
def login(request):
username = request.POST['username']
password = request.POST['password']
user = auth.authenticate(username=username, password=password)
if user is not None:
try:
auth.login(request, user)
session_key = request.session.session_key
if session_key is not None:
return HttpResponse(json.dumps({'status': 'Success',
'sessionid': session_key}))
else:
return HttpResponse(json.dumps({'status': 'Empty session key'}))
except Exception as e:
return HttpResponse(json.dumps({'status': 'Cannot log in'}))
else:
return HttpResponse(json.dumps({'status': 'Cannot authenticate'}))When I try to log in an existing user with this code, sometimes I will get the
Empty session key response. In those cases, I resend the exact same request and this time, session key is successfully set.
What is the reason for this? What am I missing here?
I am using Django Channels development server and stunnel as the SSL proxy.
Regards