django实现github第三方本地登录

1、安装

pip install social-auth-app-django

2.生成Client ID和Client Secret

3、修改setting.py

INSTALLED_APPS = [
    'account',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'social_django', # 新增
]
...
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [os.path.join(BASE_DIR, 'templates')]
        ,
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                  'django.contrib.messages.context_processors.messages',
                'social_django.context_processors.backends', # 新增
                'social_django.context_processors.login_redirect', #新增
            ],
        },
    },
]
...

AUTHENTICATION_BACKENDS = (
    'social_core.backends.github.GithubOAuth2',
    'django.contrib.auth.backends.ModelBackend',
) # 新增

SOCIAL_AUTH_URL_NAMESPACE = 'social' # 新增

# 填写Github中获取到的KEY和SECRET

SOCIAL_AUTH_GITHUB_KEY = 'b6696e45bd0162e22901'
SOCIAL_AUTH_GITHUB_SECRET = 'ba0e9d344c84bad09822cb2f5c04dbcbaa089e20'
SOCIAL_AUTH_GITHUB_USE_OPENID_AS_USERNAME = True

# 登陆成功后的回调路由
SOCIAL_AUTH_LOGIN_REDIRECT_URL = '/account' # 登陆成功之后的路由

4、同步数据库

python manage.py migrate

5、修改项目下的urls.py

#新增
url('', include('social_django.urls', namespace='social')),

6、在登陆的模板新增

<a href="{% url "social:begin" "github" %}">Github登陆</a>

7、结果演示

 

 

posted @ 2018-09-06 18:15  感哥  阅读(1044)  评论(0编辑  收藏  举报