我实现了 django-social-auth 并让它通过 Facebook 对我进行身份验证。
我希望它还可以在系统中创建一个新用户,并尽可能写下电子邮件、姓名和电话号码。
但是,每次我以用户身份登录时,它都会覆盖 root 用户而不是创建新用户?
我也在使用自定义身份验证模块,它没有在自定义模型中创建用户。
设置.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的.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)),
)
视图.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))