I am working with sessions with the database backend in an e-commerce
shop. I store the cart ID, the number of items in the cart and the
total prize in the session. But actually have two problems.
1. I set the SESSION_EXPIRE_AT_BROWSER_CLOSE = True, but Django does
not delete the session from the relation django_session when the
browser is closed. Why is that? Is that normal?
2. How can I erase the shopping cart whose ID is in the session, when
the session ends at browser close?
I guess I should execute some code similar to the following, but I do
not know where exactly:
cart = Cart.objects.get(request.session[cart_id]) # This code is
assuming that this is in a view
cart.delete()
Thank you so much. Replies will be wellcome.
1) Yes, the browser does not send a special signal to your website
saying the user has closed their browser. There is no way to detect
precisely when a user terminates their session.
2) If you want to erase carts from expired sessions, then you need to
periodically (every hour, 30 minutes) purge your sessions. This
(realistically) has to happen from outside of the webserver (google:
django celery), and should find session objects whose expiry time has
been reached, look inside the session for carts and remove the cart if
there, before finally deleting the session object.
Cheers
Tom
1. OK, so if a user closes the browser, the session is not deleted
because there is no way to notice that. However, when I use the admin
that Django provides and I logout, I can check that the session record
in the django_session table from the db is not deleted, even doing the
logout explicitly. I do not know what can be wrong. Have you got any
idea?
2. In short I will try the option of running a python script which
deletes the session information periodically through Django Celery. I
will comment how that worked.
Thank you.
On 9 abr, 11:04, Tom Evans <tevans...@googlemail.com> wrote:
AFAIK, django "flushes" the session on logout. Deleting old one and
creating a fresh one for the user. So, the user will have always a session
in the db.
> 2. In short I will try the option of running a python script which
> deletes the session information periodically through Django Celery. I
> will comment how that worked.
Expired sessions had to be deleted manually by run "cleanup" command
http://docs.djangoproject.com/en/dev/ref/django-admin/#cleanup
You can try your self, just start the development server and check
the session keys with:
>>> from django.contrib.sessions.models import Session
>>> [s.session_key for s in Session.objects.all()]
Just login and check the sessions keys. Then logout and again
check the sessions keys
Regards,
~Rolando