【问题标题】:Pipeline workflow and variables管道工作流和变量
【发布时间】:2017-12-05 14:35:29
【问题描述】:

我的网站上有 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',
)

管道.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.

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

请问,这是怎么回事?

【问题讨论】:

    标签: python-social-auth


    【解决方案1】:

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

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

    【讨论】:

    • 你的意思是从表中获取用户ID并保存到会话中?
    猜你喜欢
    • 2015-03-16
    • 2019-09-27
    • 2021-02-22
    • 1970-01-01
    • 1970-01-01
    • 2022-12-13
    • 1970-01-01
    • 1970-01-01
    • 2011-04-01
    相关资源
    最近更新 更多