一、背景
登錄注冊基礎功能雖說常見但也對於初學者來說較難,容易被勸退,使用django-rest-auth可以快速實現該功能,如何專注於業務邏輯的實現。
二、關於
- api 文檔請點 :這里
三、完整流程
-
創建項目
- 新建的項目,目錄結構如下
- 安裝相關依賴
-
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
三、測試
- 弄好了可以測試下