Pass session data to new session Django - django

Hello Iam working on Django website and I need to pass saved session data to new one after assigning a new session to the user. Example is when a player is doing an online quiz and his browser crashes, so that the data saved for the session will be assignment to a new one after re-login. I've been trying to look for possible workarounds, but I couldn't come up with an answer, as many sources state that django can't detect a new session after a crash unless the user logs out manually.

Related

Is it mandatory to use sessions in Django to stop one user's information being displayed to another?

Hi I'm very new at Django and I'm sorry if this is a stupid question.
I have created an application using Django where I have a form accepting information using the POST method. In views.py I then get the information I want and store it in a variable to be displayed back to the page.
However, when I deployed this to heroku, I noticed anyone who accessed the webpage would enter their name and their name would be visible to everyone. I didn't store it in a database, it's only stored in a variable. How do I make it so that all user entered by a user stays on only their page when they access it? Do I need to use sessions?

Last activity field in Django's user sessions

How and when exactly does last_activity in Django sessions get updated? I've been testing a Django app, and my last activity in user sessions is logged as several days ago, even though I logged in yesterday as well. What could be going on?
That's a direct result of when sessions are saved
By default, Django only saves to the session database when the session
has been modified – that is if any of its dictionary values have been
assigned or deleted:
If you want to mark a user as being active, you can place the following code in key areas of your app to mark the session as being modified so that it will be saved again in the storage
request.session.modified = True
Alternatively you can use SESSION_SAVE_EVERY_REQUEST to make sure that the session gets saved on each and every request this of course comes with an extra hit to the db.

Django tutorial login, delete data on user logging out

I am preparing a multitenant website on Django using tenant-id for identification (and not schema). Now, for tutorial, I would want to have a webpage with sample login and password (like Django CMS does). However, I don't want to store user data from that login/password combination, so that the data would be availabale for each session and as soon as the user logs out, the data deletes. Is there any application/packages that could help me with this? Otherwise, how can I do this?

Django: what are some good strategies for persisting form input across login?

I have a webapp that allows authenticated as well as anonymous users to start entering some form data. If a user is happy with his/her input, he/she can save that form to the server. This is a very similar problem to a shopping cart application that does not require login until checkout time.
For the authenticated user, implementing a save button is trivial. However for the anonymous user, the form data need to be stored somewhere while authentication is taking place, then correctly retrieved after logged in. Can someone please suggest some general strategies to go about this?
I found this link that is promising but I want to be thorough about this topic.
I think the correct way of doing this is to use django sessions. Basically each user (anonymousUser included) has a session during its stay on the website (or even more).
If you have a form that you want to store for a specific session, you can do it by using
request.session['myform'] = form
you get it by
request.session['myform']
and you can delete it using
del request.session['myform']
Basically Django pickles a dictionary of the session and saves it in a place (typically the database, but can be on other place as explained in django sessions).

Hints for the logic of django app

I'm learning Django and for this reason I'm developing an application described below.
This application allows users (authenticated and anonymous) to send message to other users.
Authenticated users can send message and can track all messages simply as all application that uses this feature. (like Facebook messages, for example)
The problem are anonymous users. I would an anonymous user can send message to other users but he can track his messages only for his session. Users can also reply to a message of an anonymous user but If an anonymous user lost his session lost also his messages.
The problem is, how can I manage anonymous user and their messages for the session only?
Django supports anonymous sessions.
If your app is relatively simple (it sounds like it is), I would do the following:
Create a standard Django user profile model and link that to the users messages but do not use OneToOne to connect to User.
Use database backed sessions (https://docs.djangoproject.com/en/dev/topics/http/sessions/#using-database-backed-sessions)
Create a temporary user profile model for anonymous users and store
their temporary profile id in their session.
Once a day delete all profile objects that do not have a user AND
whose id is not in the sessions table. A simple way (and what I would do) is have a created date/time field on the profile and just delete any profile that was created two weeks or more ago and has a null user field. I'd just crontab a django management command.
The cool thing is that if somebody registers after using the app anonymously for a while you can use their temporary profile as their profile and they keep their messages.