Python Social Auth Django 模板示例 [英] Python Social Auth Django template example

查看:42
本文介绍了Python Social Auth Django 模板示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人在模板中使用 Python Social Auth 和 Django 的开放示例?

Does someone has an open example using Python Social Auth with Django in templates?

我查看了他们的 Github 存储库,在 django 示例中,没有关于如何在模板中处理它的内容(例如登录、注销等).

I took a look in their Github repo, and in the django exmaple, there is nothing about how to deal with it in templates (e.g. doing login, logout, etc).

推荐答案

假设您遵循了 http://psa.matiasaguirre.net/docs/configuration/django.html 并且您想使用 facebook 登录.settings.py 中的后端设置应如下所示:

Let’s say you followed Python Social Auth configuration guidelines at http://psa.matiasaguirre.net/docs/configuration/django.html and you want using facebook login. Your backend settings in settings.py should look:

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

您应该注册为 facebook 开发者并创建一个应用程序,然后在您的 settings.py 文件中填写其他数据:

You should register as facebook developer and create an app and then fill in additional data in your settings.py file:

SOCIAL_AUTH_FACEBOOK_KEY = 'xxxxxxxxxxxxxx'
SOCIAL_AUTH_FACEBOOK_SECRET = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
SOCIAL_AUTH_FACEBOOK_SCOPE = ['email']

假设登录后您希望将用户重定向到会员页面,因此您将此设置添加到 settings.py 中:

Let us assume after login you want users to be redirected to members page, so you add this setting to your settings.py:

LOGIN_REDIRECT_URL = '/members'

假设您在 django 项目中创建了 login_app,并使用 home.html 模板创建了主页视图,并使用 members.html 模板创建了成员视图(您应该让模板目录工作).

Let’s say you created login_app in your django project as well as created your home view with home.html template and also created members view with members.html template (you should have your template directory working).

根据配置指南,我们的 urls.py 应该看起来:

According to configuration guidelines our urls.py should look:

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,我们会得到一个错误:

If we would try bla-bla-bla url with DEBUG=True settings, we would get an error:

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.

对于一个非常简单的测试,我们需要添加主页视图、成员视图和注销(登录已经处理),所以我们更新的 urls.py 应该看起来:

For a very simple test we need to add home view, members view and logout (login is already handled), so our updated urls.py should look:

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 文件和存在迁移文件夹,因为我使用的是 django 1.7b4 版本):

Under our login_app directory we should have files (do not pay attention to *.pyc files and migrations folder is present because I use django 1.7b4 version):

login_app/
├── admin.py
├── __init__.py
├── __init__.pyc
├── migrations
│   └── __init__.py
├── models.py
├── tests.py
├── views.py
└── views.pyc

我们的 views.py 应该是这样的:

Our views.py should look like:

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)我们可能会保留而不添加任何内容.

Other files (including models.py) we may leave without adding anything.

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

In order to login with facebook we should redirect your users to "login/facebook". So you can just add this link or button where appropriate somewhere in your home.html template:

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

按下此链接后(如果 settings.py、urls.py、views.py 没问题并且您的 facebook 应用程序配置良好)用户将使用 facebook 登录并重定向到会员页面.如果您登录到 django admin,您应该能够在 [主页 › 默认 › 用户社交身份验证 ] 下看到新条目,并在 [ 主页 › 身份验证和授权 › 用户 ] 中看到新用户.

After this link is pressed (in case settings.py, urls.py, views.py are ok and your facebook app is configured well) users will be logged in with facebook and redirected to members page. If you login to django admin, you should be able to see new entry under [ Home › Default › User social auths ] and new user in [ Home › Authentication and Authorization › Users ].

当用户通过身份验证并重定向到会员页面时,您可以获得用户的信息,例如用户名、名字、姓氏、电子邮件.您可以通过添加到您的 members.html 模板来显示该信息:

When user is authenticated and redirected to members page, you can have user’s information such as username, first name, last name, e-mail. You can display that information by adding to your members.html template:

<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 中创建了一个应用程序:

As you already noticed, for logout we made an app in our views.py:

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

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

So we can add a link in our members.html template:

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

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

And that would be enough to log out the user and redirect to initial home page.

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

This would be very simple example which may give a better understanding how to login and logout with Python Social Auth.

这篇关于Python Social Auth Django 模板示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆