Django: signal when user logs in?

前端 未结 7 1881
予麋鹿
予麋鹿 2020-11-29 19:25

In my Django app, I need to start running a few periodic background jobs when a user logs in and stop running them when the user logs out, so I am looking for an elegant way

相关标签:
7条回答
  • 2020-11-29 20:27

    You can use a signal like this (I put mine in models.py)

    from django.contrib.auth.signals import user_logged_in
    
    
    def do_stuff(sender, user, request, **kwargs):
        whatever...
    
    user_logged_in.connect(do_stuff)
    

    See django docs: https://docs.djangoproject.com/en/dev/ref/contrib/auth/#module-django.contrib.auth.signals and here http://docs.djangoproject.com/en/dev/topics/signals/

    0 讨论(0)
提交回复
热议问题