1

我的网站上有 Facebook 身份验证,但我需要用户在身份验证期间填写个人资料表单。我已经使用身份验证管道来执行此操作,但没有成功。管道正在按应有的方式调用,但结果是错误。

假设我需要他的手机号码 - 考虑它不是来自 Facebook。

请考虑:

模型.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')

我的应用程序/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.

为什么我不能访问 User 实例,因为 auth_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 回答