使用django-rest-auth自帶登錄注冊api


一、背景

  登錄注冊基礎功能雖說常見但也對於初學者來說較難,容易被勸退,使用django-rest-auth可以快速實現該功能,如何專注於業務邏輯的實現。

 

二、關於

三、完整流程

  • 創建項目
  • 新建的項目,目錄結構如下  

   

  • 安裝相關依賴
  • pip install django-rest-framework
    pip install django-rest-auth
    
    #使用django-rest-auth 自帶的登錄注冊
    pip install django-rest-auth[with_social] 

     

  • settings.py  增加
  • INSTALLED_APPS = [
        ...
        #django-rest-auth 組件
        'rest_framework.authtoken',
        'rest_framework',
        'rest_auth',
        'django.contrib.sites',
        'allauth',
        'allauth.account',
        'rest_auth.registration'
    ]
    
    SITE_ID = 1
    #若要使用mysql,修改DATABASES 設置如下
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.mysql',
            #數據庫名稱
            'NAME': 'ONLINE_AUTO_TEST',
            #數據庫用戶名
            'USER':'dj_admin',
            'PASSWORD':'123456',
            'HOST':'127.0.0.1',
            'PORT':'3306',
        }
    }

     

  • urls.py  配置路由如下
  • urlpatterns = [
        ...
        url(r'^rest-auth/', include('rest_auth.urls')),
        url(r'^rest-auth/registration/', include('rest_auth.registration.urls'))
    ]

     

  • 同步數據庫
  • python manage.py migrate

     

三、測試

  • 弄好了可以測試下

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM