Python Social Auth Django模板示例

18

有没有一个使用 Python Social Auth 和 Django 模板的开放示例?

我看了一下他们的 Github 存储库,在 Django 示例中,并没有关于如何在模板中处理它的内容(例如进行登录、注销等)。


2
像这里的模板 https://github.com/omab/python-social-auth/tree/master/examples/django_example/example/templates 一样吗? - omab
1
@omab,你能告诉我在哪里可以找到如何使用模板标签的信息吗?我似乎无法理解何时使用“social:begin”和何时使用“social:complete”。我在代码库中搜索了一下,但似乎找不到模板标签列表。 - shad0w_wa1k3r
1
除非在少数情况下(例如使用JS SDK时),否则不应使用social:completesocial:begin是开始身份验证过程的URL(无论是登录还是注册都可以)。有一些模板上下文处理器可用于简化已关联和未关联后端列表的创建,相关文档位于http://psa.matiasaguirre.net/docs/configuration/django.html#template-context-processors。 - omab
3个回答

36

假设您按照Python Social Auth配置指南在http://psa.matiasaguirre.net/docs/configuration/django.html上进行了配置,并且您想要使用Facebook登录。您的settings.py中的后端设置应如下:

AUTHENTICATION_BACKENDS = (
'social.backends.facebook.FacebookOAuth2',
'django.contrib.auth.backends.ModelBackend',
)

你需要注册成为Facebook开发者,创建一个应用程序,然后在settings.py文件中填写额外的数据:
SOCIAL_AUTH_FACEBOOK_KEY = 'xxxxxxxxxxxxxx'
SOCIAL_AUTH_FACEBOOK_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']

假设用户登录后你希望将其重定向至会员页面,因此你需要在settings.py中添加以下设置:

LOGIN_REDIRECT_URL = '/members'

假设您已在Django项目中创建了login_app,并创建了home.html模板的主页视图以及members.html模板的成员视图(您应该让模板目录正常工作)。
根据配置指导,我们的urls.py应如下所示:
from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',
    url('', include('social.apps.django_app.urls', namespace='social')),
    url(r'^admin/', include(admin.site.urls)),
)

如果我们尝试使用DEBUG=True设置访问bla-bla-bla url,将会出现错误:
Using the URLconf defined in your_project.urls, Django tried these URL patterns, in this order:
    ^login/(?P<backend>[^/]+)/$ [name='begin']
    ^complete/(?P<backend>[^/]+)/$ [name='complete']
    ^disconnect/(?P<backend>[^/]+)/$ [name='disconnect']
    ^disconnect/(?P<backend>[^/]+)/(?P<association_id>[^/]+)/$ [name='disconnect_individual']
    ^admin/
The current URL, bla-bla-bla/, didn't match any of these.

为了进行一个非常简单的测试,我们需要添加主页视图(home view)、成员视图(members view)和注销(logout)(登录已经处理好了),因此我们更新的urls.py应该如下:

from django.conf.urls import patterns, include, url
from django.contrib import admin

urlpatterns = patterns('',
    url('', include('social.apps.django_app.urls', namespace='social')),
    url(r'^admin/', include(admin.site.urls)),
    url(r'^$', 'login_app.views.home', name='home'),
    url(r'^members/', 'login_app.views.members', name='members'),
    url(r'^logout/$', 'login_app.views.logout', name='logout'),
)

在我们的login_app目录下,应该有一些文件(不用关注*.pyc文件和migrations文件夹是否存在,因为我使用的是django 1.7b4版本):
login_app/
├── admin.py
├── __init__.py
├── __init__.pyc
├── migrations
│   └── __init__.py
├── models.py
├── tests.py
├── views.py
└── views.pyc

我们的views.py应该如下所示:
from django.shortcuts import render, redirect
from django.contrib.auth import logout as auth_logout

def home(request):
    context = {}
    template = 'home.html'
    return render(request, template, context)

def members(request):
    context = {}
    template = 'members.html'
    return render(request, template, context)

def logout(request):
    auth_logout(request)
    return redirect('/')

其他文件(包括models.py)我们可以不添加任何内容。

为了使用Facebook登录,我们应该将您的用户重定向到“login/facebook”。因此,您可以在home.html模板的适当位置添加此链接或按钮:

<a href="login/facebook">Login with facebook</a>

在此链接被按下后(假设settings.py、urls.py和views.py设置正确,而且Facebook应用程序已经配置好),用户将通过Facebook登录并被重定向到会员页面。如果您登录到Django管理员界面,您应该能够在[主页 › 默认 › 用户社交认证]下看到新条目,并在[主页 › 身份验证和授权 › 用户]中看到新用户。
当用户被认证并重定向到会员页面时,您可以获得用户名、名字、姓氏和电子邮件等用户信息。您可以通过在members.html模板中添加以下内容来显示这些信息:
<p>User's name and surname: {{ user.first_name }} {{ user.last_name}}</p>
<p>Username: {{ user.username }}</p>
<p>E-mail: {{ user.email }}</p>

正如您已经注意到的,我们在views.py中创建了一个应用程序来注销:

def logout(request):
    auth_logout(request)
    return redirect('/')

因此,我们可以在members.html模板中添加一个链接:

<a href="/logout">Logout</a>

这就足以注销用户并重定向到初始主页。

这是一个非常简单的例子,可以更好地理解如何使用Python Social Auth进行登录和注销。


1
比 https://github.com/omab/python-social-auth/blob/master/examples/django_example/example/templates/home.html 更加有用。 - Robert Johnstone

13

在python-social-auth库中有一个示例。您只需要安装python-social-auth,配置您的数据库和Facebook应用程序或其他社交应用程序,并将您的Secret和key放入settings.py文件中,然后运行应用程序即可。有一个带有模板标记等内容的模板。点击这里:https://github.com/omab/python-social-auth并查看示例文件夹。


omab的这个库已经被python-social-auth替换了,它提供了一个Django的示例,但它全部都是ajax代码,有440行之多。链接在此:https://github.com/omab/python-social-auth/blob/master/examples/django_example/example/templates/home.html 。此外,在示例文件夹中还有另一个神秘命名为“django_me_example”的示例。这个“me”是什么意思? - Robert Johnstone
你能指导我将 Twitter 的密钥和秘钥放入设置文件中吗? - Jameel Grand

7

您的回答包含一个链接,如果该网站在未来某个时间停止运营则将变得无用。请考虑添加一些来自该网站的内容,使读者能够受益。 - Nisarg Shah
...并且它消失了。 - blimpse

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接