【发布时间】:2016-07-03 19:34:17
【问题描述】:
1。 我想将用户的电子邮件地址设置为 auth_user 表的用户名字段。 在字段中,first_name + last_name 设置为标准问题,例如“TomCruise”。 但是有很多人有相同的名字,特别是当我们连接到 Facebook 帐户时。 因此,我认为将电子邮件设置在用户名字段而不是 first_name + last_name 会更好。
2。 我想在 auth_user 表中添加全名字段。
from django.contrib.auth.models import AbstractUser
class User2(AbstractUser):
full_name = models.Charfield()
如果 python-social-auth 或 Django 在用户叹气时自动将 fullname(first_name + last_name) 设置为 full_name 字段,我将不胜感激。 虽然我们似乎可以从 facebook 获取用户的全名作为名称...,但我猜它是在用户名字段中推送的。
这就是我所做的。
myfacebook_pipeline.py
from myapp.models import User2
def myfacebook(backend, user, response, *args, **kwargs):
import pdb; pdb.set_trace()
if backend.name == 'facebook':
profile = User2.objects.get(id=user.id)
profile.full_name = response.get('name')
profile.username = response.get('email')
profile.save()
settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
'debug_toolbar',
'social.apps.django_app.default',
]
MIDDLEWARE_CLASSES = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.cache.UpdateCacheMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'social.apps.django_app.middleware.SocialAuthExceptionMiddleware',
]
AUTHENTICATION_BACKENDS = (
'social.backends.open_id.OpenIdAuth',
'social.backends.facebook.FacebookOAuth2',
'django.contrib.auth.backends.ModelBackend',
)
SOCIAL_AUTH_URL_NAMESPACE = 'social'
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/home/'
SOCIAL_AUTH_LOGIN_ERROR_URL = '/login-error/'
SOCIAL_AUTH_LOGIN_URL = '/'
SOCIAL_AUTH_DISCONNECT_REDIRECT_URL = '/logout/'
# facebook
SOCIAL_AUTH_FACEBOOK_KEY = 'key'
SOCIAL_AUTH_FACEBOOK_SECRET = 'password'
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']
SOCIAL_AUTH_FACEBOOK_PROFILE_EXTRA_PARAMS = {
'fields': 'id, name, email',
}
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',
'pylib.pipeline.myfacebook_pipeline.myfacebook', # <= my pipeline
'social.pipeline.social_auth.associate_user',
'social.pipeline.social_auth.load_extra_data',
'social.pipeline.user.user_details',
)
SOCIAL_AUTH_DISCONNECT_PIPELINE = (
'social.pipeline.disconnect.allowed_to_disconnect',
'social.pipeline.disconnect.get_entries',
'social.pipeline.disconnect.revoke_tokens',
'social.pipeline.disconnect.disconnect',
)
我添加了我的管道,但它不起作用。
你能给我一些建议吗?
谢谢。
【问题讨论】:
标签: python django facebook python-3.x python-social-auth