2

How can i show the time of last seen(be online)of user in django? Is there any default function or library to import it to do that?

Or if there any code inn github tell me please

**Note : ** when a user close page or disconnect the time update

Omid Reza Heidari
  • 658
  • 12
  • 27
  • 1
    You need to show us what you've tried. Your question is not very clear. – Emeka Obianom Jul 08 '17 at 08:05
  • Use javascript with one last seen update view it will be very easy – Arpit Solanki Jul 08 '17 at 08:10
  • Does this answer your question? [Django: How can I check the last activity time of user if user didn't log out?](https://stackoverflow.com/questions/14685576/django-how-can-i-check-the-last-activity-time-of-user-if-user-didnt-log-out) – gdvalderrama Jun 13 '23 at 13:00

2 Answers2

1

You should inherit django's AbstractBaseUser in your user model, it already has an inbuilt last_login attribute. In fact it is considered a good practice to inherit AbstractBaseUser for creating your user which is provided in django default auth modules.

Saurabh Agarwal
  • 116
  • 1
  • 4
  • last_login and last seen are not quite the same though, if your login sessions are long they could be very different – gdvalderrama Jun 13 '23 at 13:00
0

There is few options.

you can add a code to your views to check for user or session id (depends on how do you want it to work. for registered users or every users or ... ) and every time a user request for a page, you can update that this user has been active on the site on this time. but this option seem to be usable when you need to track a user in a small project with few pages.

another option which seem more right to do is to use middleware. by using middleware you don't need to change your code in all of your views. simply make a custom middleware and everything will be done with few lines of code

you can check an example of this middleware here:

Django: How can I check the last activity time of user if user didn't log out?

or

Django get last user visit date

and for disconnected part i think you can't do much with django. and also you can't be sure when user closed the page.

the best option here is to use a javascript code to run like every 10 sec and tell django that the user is still on the page.

Navid Zarepak
  • 4,148
  • 1
  • 12
  • 26