首次登录时创建教程

Creating Tutorial upon first login

我目前正在使用 python 社交身份验证让用户登录到我的 Django 应用程序,并在首次创建用户时显示教程。到目前为止我已经尝试过了,但是请求在 pre_save 信号中不起作用。还有另一种方法吗?

@receiver(pre_save, sender=User)
def show_tutorial(sender, instance=None, **kwargs):
    # For first time creation of user display tutorial
    if instance._state.adding:
        print ('USER CREATED')
        request.session['first_login'] = True

编辑:我在我的视图中尝试了以下代码,但一旦登录,打印语句就从未登录过终端。

def after_user_created(request, user, **kwargs):
    user, created = User.objects.get_or_create(user=user)
    if created:
         request.session['first_login'] = True
         print('FIRST LOGIN')

你做不到。即使你能做到,你也不应该去做。让我们尽可能在"view"上对"request"进行操作。像这样。

def create_view(request):
    user, created = User.objects.get_or_create(id=xxx)
    if created:
         request.session['first_login'] = True

更新

添加假设使用“social-app-django”的解决方案。

①设settings.SOCIAL_AUTH_NEW_USER_REDIRECT_URL

NEW_USER_REDIRECT_URL=/new/user/view

http://python-social-auth.readthedocs.io/en/latest/configuration/settings.html?highlight=NEW_USER_REDIRECT_URL#urls-options

② 使用新用户视图更新会话。

def new_user_view(request):
    request.session['first_login'] = True