Session key is empty after login on Django Channels development server

500 views
Skip to first unread message

Utku Gültopu

unread,
Jan 8, 2017, 4:43:55 AM1/8/17
to Django users
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

Andrew Godwin

unread,
Jan 8, 2017, 4:25:03 PM1/8/17
to django...@googlegroups.com
Hi,

Have you tried removing the "channels" package from INSTALLED_APPS and making sure it fails without that as well?

Andrew

--
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+unsubscribe@googlegroups.com.
To post to this group, send email to django...@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.

Utku Gültopu

unread,
Jan 9, 2017, 9:32:56 AM1/9/17
to Django users
It's something about Django. I don't know if it is expected behavior or not, hence I created a ticket.

Regards
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages