2

因此,正如标题所示,我可以使用 Facebook 创建一个新用户并创建一个新的个人资料。但是,我没有登录。我总是被重定向到 /login/ (最后有一些字符)。使用此代码,我会缺少什么?

我的设置.py

SOCIAL_AUTH_DEFAULT_USERNAME = lambda u: slugify(u)
SOCIAL_AUTH_UUID_LENGTH = 10
SOCIAL_AUTH_EXTRA_DATA = False
SOCIAL_AUTH_EXPIRATION = 'expires'
SOCIAL_AUTH_INACTIVE_USER_URL = '/inactive/'
SOCIAL_AUTH_RAISE_EXCEPTIONS = DEBUG
SOCIAL_AUTH_CHANGE_SIGNAL_ONLY = True
SOCIAL_AUTH_ASSOCIATE_BY_MAIL = True
SOCIAL_AUTH_COMPLETE_URL_NAME = 'socialauth_complete'
SOCIAL_AUTH_ASSOCIATE_URL_NAME = 'socialauth_associate_complete'

AUTHENTICATION_BACKENDS = (
    'social_auth.backends.facebook.FacebookBackend',
    'django.contrib.auth.backends.ModelBackend',
)

TEMPLATE_CONTEXT_PROCESSORS = global_settings.TEMPLATE_CONTEXT_PROCESSORS + (
    'social_auth.context_processors.social_auth_by_name_backends',
    'social_auth.context_processors.social_auth_backends',
    'social_auth.context_processors.social_auth_by_type_backends',
    'social_auth.context_processors.social_auth_login_redirect',
)

我的模型.py

signals.post_save.connect(create_profile, sender=User)
pre_update.connect(facebook_extra_values, sender=FacebookBackend)

class UserProfile(models.Model):
    user = models.ForeignKey(User)
    photo = ImageField(upload_to="profile_pictures")

    def get_absolute_url(self):
        return ('profiles_profile_detail', (), {'username':self.user.username})

    get_absolute_url = models.permalink(get_absolute_url)

我的接收者.py

def facebook_extra_values(sender, instance, response, details, **kwargs):
    """
    post_save signal from User model; check bzuser.models
    for the connection

    """
    from .models import UserProfile
    UserProfile(user=instance).save()
    return True

我的网址.py

urlpatterns = patterns('',
    url(r'^login/$', custom_login),
    url(r'^logout/$', custom_logout),
    url(r'^social_login/$', SocialLoginView.as_view(), name="social_login"),
    (r'', include('registration.backends.default.urls')),
    url(r'^register$', 'registration.views.register', {
        'form_class': RegistrationFormUniqueEmail,
        'backend': 'registration.backends.default.DefaultBackend'
    }, name='registration_register'),
    (r'^profiles/', include('profiles.urls')),
    url(r'^profile', ProfileView.as_view(), name="profile_private"),
)

和我的views.py

class ProfileView(TemplateView):
    template_name = 'profile.html'

class SocialLoginView(TemplateView):
    def get_context_data(self, **kwargs):
        context = super(SocialLoginView, self).get_context_data(**kwargs)
        context['title'] = _('Log In With Facebook or Twitter')
        return context

    template_name = 'social.html'

def custom_login(request):
    if request.user.is_authenticated():
        return HttpResponseRedirect(reverse('profiles_profile_detail',
            kwargs={"username":request.user.username}))
    else:
        return login(request)

def custom_logout(request):
    return logout_then_login(request)

和我的模板:

{% extends 'base.html' %}

{% block content %}
    <div class="widget">
        <div class="title">
            <img class="titleIcon" src="{{ STATIC_URL }}images/icons/dark/preview.png" />
            <h6>Social Login</h6>
        </div>

        <div class="body textC">
            <a class="wButton bluewB m10"
                href="{% url socialauth_begin 'facebook' %}{% if request.GET.next %}?next={{ request.GET.next }}{% endif %}">
                <span>Log In With Facebook</span>
            </a>
        </div>
    </div>
{% endblock %}

提前致谢!

编辑:添加了 AUTHENTICATION_BACKENDS 常量

来自应用程序的 urls.py

urlpatterns = patterns('',
    # project specific urls
    url(r'', include('base.urls')),
    url(r'', include('bzuser.urls')),

    # 3rd party URLS
    ('^nexus/', include(nexus.site.urls)),
    url(r'', include('social_auth.urls')),
)

编辑:添加了社交身份验证包含 URL

4

1 回答 1

2

我有一个类似的问题并最终解决了。您应该使用身份验证管道!并确保通过电子邮件联系。让你有类似的东西

SOCIAL_AUTH_PIPELINE = (
    'social_auth.backends.pipeline.social.social_auth_user',
    'social_auth.backends.pipeline.associate.associate_by_email',
    'social_auth.backends.pipeline.misc.save_status_to_session',
    'people.pipeline.redirect_to_get_phonenumber_form',
    'people.pipeline.phonenumber',
    'people.pipeline.save_new_persons_data',
    'social_auth.backends.pipeline.social.associate_user',
    'social_auth.backends.pipeline.social.load_extra_data',
    'social_auth.backends.pipeline.user.update_user_details',
) 
于 2013-03-17T08:30:52.443 回答