• Django之第三方登陆模块


    Django之第三方登陆模块

     前期准备

    安装 django-allauth

    pip install django-allauth

    注意,django-allauth 需要在 Django1.10以上版本使用。

    settings.py 文件配置

    INSTALLED_APPS = (
        ...
        # 需要的 app
        'django.contrib.auth',
        'django.contrib.sites',
    
        'allauth',
        'allauth.account',
        'allauth.socialaccount',
        # 提供你想接入的第三方验证账户,这里以百度为例
        'allauth.socialaccount.providers.baidu',
    )
    
    SITE_ID = 1
    
    LOGIN_REDIRECT_URL = '/'
    

      

    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'django.contrib.sites',
        'allauth',
        'allauth.account',
        'allauth.socialaccount',
          # 下面是第三方账号相关的,比如我选了weibo和github
        'allauth.socialaccount.providers.weibo',
         'allauth.socialaccount.providers.github',
    ]
     # django-allauth相关设置
    AUTHENTICATION_BACKENDS = (
          # django admin所使用的用户登录与django-allauth无关
          'django.contrib.auth.backends.ModelBackend',
          # `allauth` specific authentication methods, such as login by e-mail
          'allauth.account.auth_backends.AuthenticationBackend',
    )
    # 前面我们app里添加了django.contrib.sites,需要设置
    SITE_ID = 1
    LOGIN_REDIRECT_URL = '/'
    ACCOUNT_AUTHENTICATION_METHOD = 'username_email'
    ACCOUNT_EMAIL_REQUIRED = True
    
    # ACCOUNT_AUTHENTICATION_METHOD = 'username_email'的作用是当用户登录时,既可以使用用户名也可以使用email, 其他可选的值是 "username""email" ,ACCOUNT_EMAIL_REQUIRED = True要求用户注册时必须填写email,默认False,email是选填的。
    设置
    除了 allauth.socialaccount.providers.baidu 外,也可以把 baidu 换成 twitter 或 github 等几十种不同的网站验证
    官方网站列出了所有支持的网站:https://django-allauth.readthedocs.io/en/latest/providers.html
    

      

    urls.py文件配置

    re_path(r'^accounts/', include('allauth.urls')),
    

     

    同步数据库

    ./manage.py migrate

     

     

    开启服务

     后面先个坑!!

  • 相关阅读:
    Android 2.2 r1 API 中文文档系列(11) —— RadioButton
    Android API 中文 (15) —— GridView
    Android 中文 API (16) —— AnalogClock
    Android2.2 API 中文文档系列(7) —— ImageButton
    Android2.2 API 中文文档系列(6) —— ImageView
    Android 2.2 r1 API 中文文档系列(12) —— Button
    Android2.2 API 中文文档系列(8) —— QuickContactBadge
    [Android1.5]TextView跑马灯效果
    [Android1.5]ActivityManager: [1] Killed am start n
    Android API 中文(14) —— ViewStub
  • 原文地址:https://www.cnblogs.com/-wenli/p/10573894.html
Copyright © 2020-2023  润新知