A
A
Andrei1penguin12021-06-05 21:47:09
Django
Andrei1penguin1, 2021-06-05 21:47:09

How to access the session of a specific user?

Good day, there is a task to determine whether the user is on the site or not.
When the user enters the site, a session is created.
Actually, the task is: to access this session through the user, but I don’t really understand how this can be done
Alternatively, it's probably possible to change the user's is_active value to True or False every time they enter and exit the site, but I suspect that this is not the best idea

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Gornostaev, 2021-06-05
@sergey-gornostaev

Django runs over HTTP, which is a stateless protocol. Without WebSockets or front-end polling, there's no way you can reliably tell if a user is on the site.

D
Dmitry, 2021-06-06
@pyHammer

Andrei1penguin1 you can try to do something like this, in the user model create a field "last visit time", create an online method there to check this field, if it is greater than the current time minus 5 minutes, then the user is online. This field can be updated in such a way that, through middleware, update this field with each request, and also place a script in html that will send a request to the server for some minimal page at intervals of 1 minus, thus updating the field of the last visit to the site

M
maksam07, 2021-06-07
@maksam07

Take the implementation of "users online" for any PL (there are many options in php) and stuff it into django. The scheme is the same everywhere.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question