1

サイトで Facebook 認証が機能していますが、認証中にユーザーがプロファイル フォームに入力する必要があります。そのために認証パイプラインを使用しましたが、成功しませんでした。パイプラインは正常に呼び出されていますが、結果はエラーです。

彼の携帯電話番号が必要だとしましょう。Facebook からのものではないことを考慮してください。

考えてください:

models.py

from django.db import models
from django.contrib.auth.models import User

class Profile(models.Model):
    user = models.OneToOneField(User)
    mobile = models.IntegerField()

設定.py

SOCIAL_AUTH_PIPELINE = (
    'social.pipeline.social_auth.social_details',
    'social.pipeline.social_auth.social_uid',
    'social.pipeline.social_auth.auth_allowed',
    'social.pipeline.social_auth.social_user',
    'social.pipeline.user.get_username',
    'social.pipeline.mail.mail_validation',
    'social.pipeline.user.create_user',
    'social.pipeline.social_auth.associate_user',
    'social.pipeline.social_auth.load_extra_data',
    'social.pipeline.user.user_details',
    'myapp.pipeline.fill_profile',
)

パイプライン.py

from myapp.models import Profile
from social.pipeline.partial import partial

@partial
def fill_profile(strategy, details, user=None, is_new=False, *args, **kwargs):
    try:
        if user and user.profile:
            return
        except:
            return redirect('myapp.views.profile')

myapp/views.py

from django.shortcuts import render, redirect 
from myapp.models import Perfil

def profile(request):
    if request.method == 'POST':
        profile = Perfil(user=request.user,mobile=request.POST.get('mobile'))           
        profile.save()
        backend = request.session['partial_pipeline']['backend']
        redirect('social:complete', backend=)
    return render(request,'profile.html')

これprofile.htmlは、「mobile」という名前の入力テキスト ボックスと送信ボタンを備えた単なるフォームです。

次に、次のエラーが表示されます。

Cannot assign "<SimpleLazyObject: <django.contrib.auth.models.AnonymousUser object at 0x03C2FB10>>": "Profile.user" must be a "User" instance.

auth_user テーブルのユーザーが既に存在するため、User インスタンスにアクセスできないのはなぜですか?

お願いします、これの何が問題なのですか?

4

1 に答える 1

1

request.userまだログインしていないため、ユーザーにアクセスすることはできません。ユーザーは、パイプラインの実行後にソーシャル完全ビューでログインされます。通常、部分パイプライン ビューはフォーム データをセッションに保存し、パイプラインはそれを選択して保存します。また、パイプラインのセッションでユーザー ID を設定し、ビューでその値を選択することもできます。例えば:

@partial
def fill_profile(strategy, user, *args, **kwargs):
    ...
    strategy.session_set('user_id', user.id)
    return redirect(...)
于 2014-02-20T16:49:02.707 に答える