3

Im using Mongo Engine/REST Framework with Django 1.6.

All I need is for the client to get a sessionid when he hits one of the views. However at the moment no sessionid is present in the cookie.

views.py

@csrf_exempt
def updateInput(request):
    return HttpResponse("view has been hit")

settings.py

MIDDLEWARE_CLASSES = Common.MIDDLEWARE_CLASSES + ('django.contrib.sessions.middleware.SessionMiddleware',)
INSTALLED_APPS += ('django.contrib.sessions',)

SESSION_ENGINE = 'mongoengine.django.sessions'
SESSION_SERIALIZER = 'mongoengine.django.sessions.BSONSerializer'

DJANGO_APPS = (
    'django.contrib.sessions',
)

Any ideas ?

ferrangb
  • 2,012
  • 2
  • 20
  • 34
felix001
  • 15,341
  • 32
  • 94
  • 121

2 Answers2

6

According to the Django docs, a user has no session until it is necessary:

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

You need to set some session data, then you will find the sessionid in the cookie.

Martijn Arts
  • 723
  • 7
  • 22
  • This worked on the view. But on my other view the key returns null even though i perform the session key update and lookup for the session id before any serialization is performed (??) – felix001 Feb 24 '15 at 16:11
  • Still having issues but updated question here http://stackoverflow.com/questions/28713650/django-rest-mongodb – felix001 Feb 25 '15 at 07:40
  • 1
    The Django docs Link doesn't work: [working Django docs](https://docs.djangoproject.com/en/3.1/topics/http/sessions/#technical-details) – Pankov Jan 01 '21 at 09:30
  • Thanks @Pankov, I've fixed the link :) – Martijn Arts Jan 04 '21 at 14:00
1

I know that this answer won't help you, but will definitely help those like I came across your question. If you are using the cookie from javascript, for example as part of an ajax request, you will need to set the following in your settings.py

SESSION_COOKIE_HTTPONLY=False
Rui Lima
  • 7,185
  • 4
  • 31
  • 42