request.session is not saved in Django
Asked Answered
S

3

8

I have a pretty simply utility function that gets an open web order if their is a session key called 'orderId', and will create one if there is no session key, and the parameter 'createIfNotFound' is equal to true in the function. Stepping through it with my debugger I can see that the piece of code that sets the session key after an order has been created does get hit with no exceptions, but when I check the Http request object' session field, it does not have that attribute ?

Utility

def get_open_web_order(request, createIfNotFound=False):
    # Check for orderId in session
    order_id = request.session.get('orderId')
    web_order = None

    if None != order_id:
        try:
            web_order = WebOrder.objects.get(id=order_id, status='O')
            logging.info('Found open web order')
        except WebOrder.DoesNotExist:
            logging.info('Web order not found')

    if (None == web_order) and (createIfNotFound == True):
        logging.info('Creating new web order')

        web_order = WebOrder()
        web_order.status = 'O'

        web_order.save()
        request.session['orderId'] = web_order.id

        # Assign logged in user and default billing and shipping
        if request.user.is_authenticated() and hasattr(request.user, 'customer'):
            customer = request.user.customer
            web_order.customer = customer
            web_order.set_defaults_from_customer()
            web_order.save()

    return web_order
Scapegrace answered 12/9, 2016 at 19:22 Comment(1)
This was happening to me with an embedded Shopify app. Safari blocks 3rd party cookies by default, and disabling this feature seemed to fix the issue.Fret
A
19

In some cases you need to explicitly tell the session that it has been modified.

You can do this by adding request.session.modified = True to your view, after changing something in session

You can read more on this here - https://docs.djangoproject.com/en/1.10/topics/http/sessions/#when-sessions-are-saved

Acth answered 12/9, 2016 at 19:43 Comment(1)
It's curious this solved the issue, since according to the documentation, the kind of change OP should be detected as session-modifying and trigger a save, so an explicit set of the .modified attribute should not be needed. Regardless, I'm running into a similar issue, and will update if this fixes my issue as well.Melliemelliferous
F
3

I had a similar issue, turns out I had set SESSION_COOKIE_DOMAIN in settings.py to the incorrect domain so it would not save any of my new session data. If you are using SESSION_COOKIE_DOMAIN, try checking that!

For example, if I am running the server on my localhost but I have in my settings SESSION_COOKIE_DOMAIN = "notlocalhost", then nothing I change in request.session will save.

Foredo answered 25/2, 2021 at 19:33 Comment(0)
C
0

For example, putting request.session.modified = True (False by default) anywhere in test() can save session as shown below. *Session is automatically saved only when top-level key's data is modified and you can see When sessions are saved and you can see my question and my answer explaining when and where session is saved:

# "veiws.py"

from django.http import HttpResponse

def test(request):
    request.session.modified = True # Here
    return HttpResponse('Test')

Or, setting SESSION_SAVE_EVERY_REQUEST True in settings.py as shown below also can save session:

# "settings.py"

SESSION_SAVE_EVERY_REQUEST = True
Clink answered 8/7, 2023 at 6:8 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.