我的网站上有 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 表中的用户已经存在(我想)?
请问,这有什么问题吗?