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

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

问题描述

有没有人在模板中使用 Python Social Auth 与Django打开一个例子?



我看了他们的Github回购,在django的例子中,没有任何关于如何在模板中处理它(例如登录,注销等)。

解决方案

我们假设您按照 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应该看起来:



<$ p $来自django.conf.urls导入模式的p> 包含来自django.contrib的url
import admin

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

如果我们尝试使用bla-bla-bla url与DEBUG = True设置,我们会得到一个错误:

 使用your_project.urls中定义的URLconf,Django按以下顺序尝试了这些URL模式:
^ 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 /
当前的URL,bla-bla-bla /,与之不匹配。

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

  from django.conf.urls import patterns,include,url 
从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版本):

  login_app / 
├──admin.py
├──__init__.py
├──__init __。pyc
├──迁移
│└──__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(请求,模板,上下文)

def成员(请求):
context = {}
template ='members.html'
返回渲染(请求,模板,上下文)

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

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



为了登录Facebook,我们应该将您的用户重定向到登录/ Facebook 。因此,您可以在home.html模板中的某个地方添加此链接或按钮:

 < a href =login / facebook>使用Facebook登录< / a> 

按此链接后(在settings.py,urls.py,views.py都可以)并且您的Facebook应用程序配置良好)用户将使用Facebook登录并重定向到会员页面。如果您登录到django管理员,您应该可以在[主页>默认>用户社交认证]和新用户[主页>认证和授权>用户]中查看新条目。



当用户被认证并重定向到成员页面时,您可以拥有用户名,姓名,姓氏,电子邮件等用户信息。您可以通过添加到您的members.html模板来显示该信息:

 < p>用户名和姓:{{user .first_name}} {{user.last_name}}< / p> 
< p>用户名:{{user.username}}< / p>
< p>电子邮件:{{user.email}}< / p>

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

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

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

 < a href =/ logout>注销< / a> 

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



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


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

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).

解决方案

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',
)

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']

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'

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).

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)),
)

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.

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'),
)

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

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('/')

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

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>

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 ].

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>

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

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

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.

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天全站免登陆