Skip to main content

Django app which keep track of user login history.

Project description

dj-user-login-history

A Django app which keep track of user login history.

How to install

pip install dj_user_login_history

Usage

  1. Add login_history app to INSTALLED_APPS
INSTALLED_APPS = [
    ...
    'login_history',
    ...
]
  1. Migrate database:
python manage.py migrate
  1. Now all your user's login history will be tracked to a model called LoginHistory
  2. You can check a user's active logins:
user.active_logins
  1. You can add login_history.urls to your project url to see an example page explaining the user login histores.
  2. For better understanding, read following code:
def login_histories(request):

    if not request.user.is_authenticated:
        return HttpResponse("<h1>Please login to see your login histories</h1>")\
        
    active_logins = request.user.active_logins

    active_logins_html = ""
    for login in active_logins:
        active_logins_html += f'<li>{login.ip} - {login.date_time} - {login.user_agent}</li>'

    return HttpResponse(
    f"""
        <h1>Active Logins</h1>
        <ul>
            {active_logins_html}
        </ul>
    """
    )
  1. Use the ip to grab more information directly from the Frontend instead of backend, it'll optimize performance. There're huge JS library which do the work, use any one of them.

NB: When you write your own login view, make sure you log the user in using django login

from django.contrib.auth import login as django_login

django_login(request, user)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dj-user-login-history-1.0.6.tar.gz (6.0 kB view hashes)

Uploaded Source

Built Distribution

dj_user_login_history-1.0.6-py3-none-any.whl (9.0 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page