【问题标题】:Doing certain actions upon user creation in Django在 Django 中创建用户时执行某些操作
【发布时间】:2017-08-08 14:18:22
【问题描述】:

我正在使用 social-auth-app-django 与 Facebook 和 Google 进行社交身份验证。一旦创建用户以执行某些命令,例如在用户创建后显示教程(仅一次),我正在尝试创建接收器。

以下是我正在尝试做的,但无法让它工作。

from social_core.pipeline.user import create_user

@receiver(create_user)
def after_user_created(request, user, **kwargs):
    userprofile = UserProfile.objects.create(user=user)
    userprofile.full_name = user.first_name + " " + user.last_name
    userprofile.save()
    request.session['first_login'] = True
    return render_to_response("main/highlights/highlights.html",locals(),RequestContext(request))

基本上,我如何创建某种接收器以在创建唯一用户后激活,以便在创建该用户后执行自定义操作?我当前的代码似乎不起作用。

【问题讨论】:

    标签: python django facebook python-social-auth receiver


    【解决方案1】:

    在你的应用models.py中写一个如下所示的预保存信号:

    @receiver(pre_save, sender=User)
    def show_tutorial(sender, instance=None, **kwargs):
        # For first time creation of user display tutorial
        if instance._state.adding:
            # display tutorial() 
    

    【讨论】:

    • 我试着按照你说的做,然后放一个 request.session['first_login'] = True,你放了 #display tutorial () 但我收到了这个错误:缺少 1 个必需的位置参数: '请求'
    猜你喜欢
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    • 2022-01-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    相关资源
    最近更新 更多