Search code examples
pythondjangocookiesdjango-sessions

Django 1.11.1 No SessionID generated on The Project


I am trying to build a app using django. I created the app using django-admin startproject. On viewing the page in the browser no session cookie is set.

On checking the response in chrome dev-tools i found that the response has no set cookie for session.

I tried adding 'SESSION_SAVE_EVERY_REQUEST = True' in settings.py but the same problem.

settings.py file code

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

Response Header form testserver

HTTP/1.0 200 OK
Date: Thu, 18 May 2017 07:23:00 GMT
Server: WSGIServer/0.1 Python/2.7.12
X-Frame-Options: SAMEORIGIN
Content-Type: text/html
Content-Length: 1716

Solution

  • it's because

    Django only sends a cookie if it needs to. If you don’t set any session data, it won’t send a session cookie.

    https://docs.djangoproject.com/en/1.11/topics/http/sessions/

    So one one of the pages that you are visiting arrange to make a change to the session eg

    request.session['hello'] = 'world'
    

    after that you will see the session hearder everywhere.