2014-02-19 1 views
0

내 사이트에서 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() 

settings.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', 
) 

pipeline.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'이라는 입력 텍스트 상자와 제출 버튼이있는 양식입니다.

가 그럼 난이 오류 : AUTH_USER 테이블에 사용자가 이미 있기 때문에

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

가 왜 사용자 인스턴스에 액세스 할 수 없습니다 (나는 가정한다)?

제발,이게 뭐가 잘못 됐니?

답변

1

아직 로그인하지 않았기 때문에 request.user에서 사용자에게 액세스 할 수 없습니다. 사용자는 파이프 라인 실행 후 소셜 완료보기에 로그인됩니다. 일반적으로 부분 파이프 라인 뷰는 양식 데이터를 세션에 저장 한 다음 파이프 라인에서 선택하고 저장합니다. 또한 파이프 라인의 세션에서 사용자 ID를 설정 한 다음보기에서 해당 값을 선택할 수 있습니다. 예 :

@partial 
def fill_profile(strategy, user, *args, **kwargs): 
    ... 
    strategy.session_set('user_id', user.id) 
    return redirect(...) 
+0

테이블에서 사용자 ID를 가져 와서 세션에 저장한다는 의미입니까? – PedroBoi

+0

코드 스 니펫을 추가하고 답변에 대한 업데이트를 확인하십시오. – omab