【发布时间】:2014-09-19 22:13:05
【问题描述】:
我想在身份验证管道的末尾添加一个函数,该函数旨在检查该用户是否有一个“配置文件”表,如果没有,它将创建一个表。 Profiles 模型是一个表,我在其中存储了一些关于用户的额外信息:
class Profiles(models.Model):
user = models.OneToOneField(User, unique=True, null=True)
description = models.CharField(max_length=250, blank=True, null=True)
points = models.SmallIntegerField(default=0)
posts_number = models.SmallIntegerField(default=0)
每个用户都必须有一个 Profiles 表。所以,我在流水线的末尾添加了一个函数:
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.user.create_user',
'social.pipeline.social_auth.associate_user',
'social.pipeline.social_auth.load_extra_data',
'social.pipeline.user.user_details',
'app.utils.create_profile' #Custom pipeline
)
#utils.py
def create_profile(strategy, details, response, user, *args, **kwargs):
username = kwargs['details']['username']
user_object = User.objects.get(username=username)
if Profiles.ojects.filter(user=user_object).exists():
pass
else:
new_profile = Profiles(user=user_object)
new_profile.save()
return kwargs
我得到错误:
KeyError at /complete/facebook/
'details'
...
utils.py in create_profile
username = kwargs['details']['username']
我是 python social auth 的新手,看起来我遗漏了一些明显的东西。任何帮助将不胜感激。
【问题讨论】:
-
从您显示的错误跟踪中,似乎 kwargs 缺少密钥
details -
嗯,是的,这就是问题所在。我正在关注this 教程,因为他做了
email = kwargs['details']['email']我想我可以做username = kwargs['details']['username']。无论如何,我通过username = details['username']解决了它。但是现在我有一个新问题......'用户匹配查询不存在'。 :P...无论如何。谢谢。
标签: python django python-social-auth