How to prevent multiple login in Django

前端 未结 2 1659
我寻月下人不归
我寻月下人不归 2020-12-06 07:50

I\'m writing a User system that cannot login at the same time. If the account in login state in somewhere, and someone login the same account in other position. The latter o

2条回答
  •  無奈伤痛
    2020-12-06 08:19

    Since you want the user to have only one session at a time, you can call logout before you call login

    ...
    if user is not None and user.is_active:
        auth.logout(request)
        auth.login(request, user)
    

    Logout documentation: https://docs.djangoproject.com/en/3.0/topics/auth/default/#django.contrib.auth.logout

提交回复
热议问题