2017-03-17 120 views
0

我使用social-auth-app-django與Facebook和Google進行社交身份驗證。我嘗試創建一個接收器,一旦創建用戶來執行某些命令,例如在用戶創建後顯示教程(僅一次)。在Django中創建用戶時執行某些操作

下面是我想要做的,但一直沒有得到它的工作。

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)) 

從本質上講,我怎麼能建立某種形式的接收器的激活一次我創建一個唯一的用戶,這樣一旦用戶已經創建我可以進行自定義操作?我目前的代碼似乎沒有工作。

回答

0

在你的應用程序的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() 
+0

我試圖做你所說的話,並把一個的request.session [「first_login」] = TRUE,在那裏你把#顯示教程(),但我得到這個錯誤:缺少1所需的位置參數:'請求' – Viji123

相關問題