2017-03-18 12 views
0

私は現在、ユーザーをDjangoアプリケーションにログインさせ、最初のユーザー作成時にチュートリアルを表示するためにpython social authを使用しています。私はこれまでこれを試しましたが、要求は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 

EDIT:私の意見には次のコードを試しましたが、一度ログインすると、printステートメントは端末に記録されませんでした。

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

答えて

1

あなたはできません。あなたがそれをしてもそれをしてはいけません。できるだけ "要求"の操作を "ビュー"で行ってみましょう。このような。

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

UPDATE

"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 
+0

上記の編集内容を確認してください。私はこれを試しましたが、なんらかの理由でプリントステートメントが端末に表示されなかったため、リクエストが処理されませんでした。 – Viji123

+0

@ Viji123これはあなたが使っているライブラリですか? https://github.com/python-social-auth/social-core –

+0

いいえ、私はhttps://github.com/python-social-auth/social-app-djangoを使用しています – Viji123

関連する問題