2014-02-19 99 views
0

我在我的网站上有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() 

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'的输入文本框和提交按钮的表单。

然后我得到这个错误:

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

为什么我不能访问用户实例,因为在AUTH_USER表中的用户已经存在(我想)?

请问这有什么问题?

回答

1

您不能访问request.user中的用户,因为它尚未登录,用户将在执行管道后登录社会完整视图。通常,部分管道视图会将表单数据保存到会话中,然后管道将选择并保存它。您也可以在管道中的会话中设置用户标识,然后在视图中选择该值。例如:

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

您的意思是从表中获取用户标识并保存到会话中? – PedroBoi

+0

我已经添加了一段代码,检查我的更新到答案。 – omab