saving django session data for anonymous user

771 views
Skip to first unread message

Siddharth Srivastava

unread,
Jun 12, 2018, 9:36:13 AM6/12/18
to Django users
Hi ,

i was writing small ecommerce application in django framework. so i was trying to implement add to cart functionality like that if user is anonymous user then selected products should be mapped to it's session id which is act. cart id in models. so the scenario is that whenever anonymous user check out and mapped data against that session id is purged. Is there any mechanism to save anonymous user session data even after user logs in. kindly provide easy possible solution as i am bit new to django:)

Thanks,

Siddharth


Anthony Flury

unread,
Jun 12, 2018, 11:38:38 AM6/12/18
to Siddharth Srivastava, django...@googlegroups.com
The only way I can think of is when your user goes to log in - check if
their session id is already recorded.

When they login - I assume that they get an new Session Id - and remap
the data from their old session id to their new session Id.

Would that work ?
> --
> 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...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto: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/d17bbc16-4c0b-4d06-bfa5-af814a20cbc6%40googlegroups.com
> <https://groups.google.com/d/msgid/django-users/d17bbc16-4c0b-4d06-bfa5-af814a20cbc6%40googlegroups.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.


--
--
Anthony Flury
email : *Anthon...@btinternet.com*
Twitter : *@TonyFlury <https://twitter.com/TonyFlury/>*

Melvyn Sopacua

unread,
Jun 12, 2018, 11:42:53 AM6/12/18
to django...@googlegroups.com

On dinsdag 12 juni 2018 12:01:32 CEST Siddharth Srivastava wrote:

 

> so the scenario is that whenever anonymous

> user check out and mapped data against that session id is purged. Is there

> any mechanism to save anonymous user session data even after user logs in.

> kindly provide easy possible solution as i am bit new to django:)

 

 

Well, the easy solution is to follow the documentation:

 

Note that any data set during the anonymous session is retained in the

session after a user logs in.

 

--

Melvyn Sopacua

Siddharth Srivastava

unread,
Jun 13, 2018, 8:40:37 AM6/13/18
to Anthony Flury, django...@googlegroups.com
Thanks  Anthony for your prompt reply. current implementation is that i am using django orm query to pull the user cart details and cart id(session key) is the key so when user switch from anonymous
user to actual user then i loses the data came from the query. 

so when user gets authenticated then it create new session id then previous session data we lose. I am not finding much help in google:(.

Thanks,

Siddharth



On Tue, Jun 12, 2018 at 8:37 AM, Anthony Flury <anthon...@btinternet.com> wrote:
The only way I can think of is when your user goes to log in - check if their session id is already recorded.

When they login - I assume that they get an new Session Id - and remap the data from their old session id to their new session Id.

Would that work ?

On 12/06/18 11:01, Siddharth Srivastava wrote:
Hi ,

i was writing small ecommerce application in django framework. so i was trying to implement add to cart functionality like that if user is anonymous user then selected products should be mapped to it's session id which is act. cart id in models. so the scenario is that whenever anonymous user check out and mapped data against that session id is purged. Is there any mechanism to save anonymous user session data even after user logs in. kindly provide easy possible solution as i am bit new to django:)

Thanks,

Siddharth


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

Anthony Flury

unread,
Jun 15, 2018, 3:31:28 AM6/15/18
to Siddharth Srivastava, django...@googlegroups.com
How are you authenticating them ?

Assuming you call :

        authenicate( username, password)

then before that call - store the sesssion key

after that call - identify the new session key

with the old and new session keys - now update the database - so that
the cart gets associated with the new session key.


On 13/06/18 13:40, Siddharth Srivastava wrote:
> Thanks  Anthony for your prompt reply. current implementation is that
> i am using django orm query to pull the user cart details and cart
> id(session key) is the key so when user switch from anonymous
> user to actual user then i loses the data came from the query.
>
> so when user gets authenticated then it create new session id then
> previous session data we lose. I am not finding much help in google:(.
>
> Thanks,
>
> Siddharth
>
>
>
> On Tue, Jun 12, 2018 at 8:37 AM, Anthony Flury
> <anthon...@btinternet.com <mailto:anthon...@btinternet.com>>
> wrote:
>
> The only way I can think of is when your user goes to log in -
> check if their session id is already recorded.
>
> When they login - I assume that they get an new Session Id - and
> remap the data from their old session id to their new session Id.
>
> Would that work ?
>
> On 12/06/18 11:01, Siddharth Srivastava wrote:
>
> Hi ,
>
> i was writing small ecommerce application in django framework.
> so i was trying to implement add to cart functionality like
> that if user is anonymous user then selected products should
> be mapped to it's session id which is act. cart id in models.
> so the scenario is that whenever anonymous user check out and
> mapped data against that session id is purged. Is there any
> mechanism to save anonymous user session data even after user
> logs in. kindly provide easy possible solution as i am bit new
> to django:)
>
> Thanks,
>
> Siddharth
>
>
> --
> 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...@googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>
> <mailto:django-users...@googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>>.
> To post to this group, send email to
> django...@googlegroups.com
> <mailto:django...@googlegroups.com>
> <mailto:django...@googlegroups.com
> <mailto:django...@googlegroups.com>>.
> <https://groups.google.com/group/django-users>.
> <https://groups.google.com/d/msgid/django-users/d17bbc16-4c0b-4d06-bfa5-af814a20cbc6%40googlegroups.com?utm_medium=email&utm_source=footer
> <https://groups.google.com/d/optout>.
>
>
>
> --
> --
> Anthony Flury
> email : *Anthon...@btinternet.com
> <mailto:Anthon...@btinternet.com>*
> Twitter : *@TonyFlury <https://twitter.com/TonyFlury/
> <https://twitter.com/TonyFlury/>>*

Melvyn Sopacua

unread,
Jun 21, 2018, 2:01:55 PM6/21/18
to django...@googlegroups.com
On woensdag 13 juni 2018 14:40:05 CEST Siddharth Srivastava wrote:
> Thanks Anthony for your prompt reply. current implementation is that i am
> using django orm query to pull the user cart details and cart id(session
> key) is the key so when user switch from anonymous
> user to actual user then i loses the data came from the query.
>
> so when user gets authenticated then it create new session id then previous
> session data we lose. I am not finding much help in google:(.

This is a very bad design. Why don't simply store the cart ID in the session,
rather then using a session ID for a cart's ID? This is just one of the
problems you're going to face.
For example, implementing abandoned cart reminders is going to be no joyride
and pretty much standard functionality in e-commerce these days. Sessions are
there to store data and Django automatically copies the data in the session
from anonymous user to logged in user. It's what sessions are made for.

--
Melvyn Sopacua
Reply all
Reply to author
Forward
0 new messages