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表中的用戶已經存在(我想)?
請問這有什麼問題?
您的意思是從表中獲取用戶標識並保存到會話中? – PedroBoi
我已經添加了一段代碼,檢查我的更新到答案。 – omab