私はdjango-social-authを実装し、Facebookで認証するためにそれを取得しました。
また、システムに新しいユーザーを作成し、可能であればメールアドレス、名前、電話番号を書き込んでもらいたいと思います。
ただし、ユーザーでログインするたびに、rootユーザーが上書きされ続け、新しいユーザーは作成されませんか?
カスタム認証モジュールも使用していますが、カスタムモデルでユーザーを作成していません。
Settings.py:
MIDDLEWARE_CLASSES = (
'django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
)
ROOT_URLCONF = 'something.urls'
WSGI_APPLICATION = 'something.wsgi.application'
TEMPLATE_DIRS = (
templatedir
)
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
'frontpage',
'social_auth',
'django.contrib.admin'
)
AUTHENTICATION_BACKENDS = (
'something.auth_backends.CustomUserModelBackend',
'social_auth.backends.twitter.TwitterBackend',
'social_auth.backends.facebook.FacebookBackend',
)
CUSTOM_USER_MODEL = 'accounts.CustomUser'
SOCIAL_AUTH_NEW_ASSOCIATION_REDIRECT_URL = '/newuser/'
SOCIAL_AUTH_DISCONNECT_REDIRECT_URL = '/disconnected/'
SOCUAL_AUTH_USER_MODEL = 'accounts.CustomUser'
SOCIAL_AUTH_CREATE_USERS = True
FACEBOOK_APP_ID = 'XXX'
FACEBOOK_API_SECRET = 'XXX'
FACEBOOK_EXTENDED_PERMISSIONS = ['email','user_birthday','user_hometown','user_location','user_about_me']
LOGIN_URL = '/'
LOGIN_REDIRECT_URL = '/home/'
LOGIN_ERROR_URL = '/login-error/'
SOCIAL_AUTH_DEFAULT_USERNAME = 'social_auth_user'
SOCIAL_AUTH_ENABLED_BACKENDS = ('twitter','facebook','google')
TWITTER_CONSUMER_KEY = 'XXX'
TWITTER_CONSUMER_SECRET = 'XXX'
URL's.py
from django.conf.urls import patterns, include, url
from django.contrib.auth.views import login, logout
from frontpage.views import LoginPage, FrontPageView
from frontpage import views
from django.contrib import admin
from social_auth import __version__ as version
from social_auth.utils import setting
admin.autodiscover()
urlpatterns = patterns('',
url(r'', include('social_auth.urls')),
url(r'^$', views.LoginPage, name='LoginPage'),
url(r'^login/$', views.loginreq, name='loginreq'),
url(r'^newuser/$', views.newuser, name='newuser'),
url(r'^logout/$', views.logout, name='logout'),
url(r'^home/$', views.HomePage, name='HomePage'),
url(r'^jobs/offers$', views.jobsoffers, name='jobsoffers'),
url(r'^jobs/view$', views.jobsview, name='jobsview'),
#url(r'^home/$', FrontPageView.as_view(), name='front_list'),
(r'^admin/jsi18n/$', 'django.views.i18n.javascript_catalog'),
# Examples:
# url(r'^$', 'something.views.home', name='home'),
# url(r'^something/', include('something.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
)
Views.py
class FrontPageView(ListView):
context_object_name = 'front_list'
template_name = 'homepage.html'
queryset = classifield.objects.all()
def get_context_data(self, **kwargs):
context = super(FrontPageView, self).get_context_data(**kwargs)
context['offers'] = offers.objects.all()
return context
def logout(request):
auth_logout(request)
return HttpResponseRedirect('/')
def LoginPage(request):
classifield_list = classifield.objects.all()
offers_list = offers.objects.all()
return render_to_response("login.html", {'classifield_list': classifield_list,'offers_list':offers_list}, context_instance=RequestContext(request))
@login_required
def HomePage(request):
classifield_list = classifield.objects.all()
ctx = {'last_login':request.session.get('social_auth_login_backend')}
return render_to_response("homepage.html", {'classifield_list': classifield_list, 'ctx':ctx}, context_instance=RequestContext(request))