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 
<https://www.stunnel.org/> as the SSL proxy.

Regards

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/71a47486-fdad-4da3-8baa-608ce66e8676%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to