一、概述在项目开发中,很多开发者都使用cookiecutter来构建Django项目的初始化模板,这样可以节省大量的时间和精力,使开发速度更快。而cookiecutter中设置的用户注册认证登录模块django-allauth对整个模块进行了封装,对于前后端不分离的项目更加友好。但是,如果前后端项目分离,很多API就不能使用,给开发带来很大的问题。为了解决这个问题,django-rest-auth应运而生,并开放了一些用户管理功能的API:激活用户注册登录注销获取或更新某个用户模型~~~~密码修改使用邮箱重设密码社交媒体认证结构:rest_auth:登录、注销、修改密码、重置密码的基本功能方法rest_auth_registration:注册和社交媒体认证的相关逻辑2.导入和配置(1)只使用django-rest-auth导入:pipenvinstalldjango-rest-auth在THIRD_INSTALLED_APPS或INSTALLED_APPS中注册rest_auth在项目迁移的一级路由中配置相应的路由url(r'^rest-auth/',include('rest_auth.urls'))执行数据:pipenvrunpythonmanage.pymigrate(2),使用allauth中的标准注册功能导入:pipenvinstalldjango-rest-auth[with_social]这里需要特别注意:如果终端使用zsh,必须使用引号把django-rest-auth[with_social]括起来,如果不括起来会报错:zsh:nomatchesfound:django-rest-auth[with_social]registereddjango.contrib.sites,allauth,allauth.account,rest_auth和rest_auth.registration到INSTALLED_APPS或者THIRD_INSTALLED_APPS中并在配置文件base.py/settings.py中设置SITE_ID=1并在项目级路由中配置对应的路由url(r'^rest-auth/',include('rest_auth.urls')),url(r'^rest-auth/registration/',include('rest_auth.registration.urls'))注意:路由中的rest_auth名称不固定,可以修改执行数据迁移:pipenvrunpythonmanage.pymigrate(3),注册账号url:rest_auth/registration/参数:usernamepassword1password2emailsetEMAIL_BACKEND='django.core.mail.backends.console.EmailBackend'request###RegistrationPOSThttp://127.0.0.1:8000/auth/registration/HTTP/2.0Content-Type:application/json{"username":"liquhua008","password1":"liqh930215","password2":"liqh930215","email":"695762725@234523.com"}Content-Type:application/json必须写,否则程序会报415错误HTTP/1.1415UnsupportedMediaTypeDate:Thu,03Dec202002:23:15GMTServer:WSGIServer/0.2CPython/3.7.0Content-Type:application/jsonVary:AcceptAllow:POST,OPTIONSX-Frame-Options:DENYContent-Length:62X-Content-Type-Options:nosniffReferrer-Policy:同源{“详细信息”:“不支持的媒体类型\”text/plain\"inrequest."}reportconnectionrefusederrororCSRFerrorreason:Tokenpermissionisnotset解决方法:设置权限在INSTALLEDAPPS中添加'rest_framework.authtoken'setREST_FRAMEWORKREST_FRAMEWORK={'DEFAULT_AUTHENTICATION_CLASSES':['rest_framework.authentication.TokenAuthentication',]}创建成功后,在终端打印出邮件内容,返回key{"key":"06e7a7767b5da07257297941ac92}b"2(4)用户url:rest_auth/login/parameter:usernamepasswordemailContent-Type:application/json成功登录返回keyHTTP/1.1200OKDate:Thu,03Dec202002:41:39GMTServer:WSGIServer/0.2CPython/3.7.0Content-Type:application/jsonVary:Accept,CookieAllow:POST,OPTIONSX-Frame-Options:DENYContent-Length:50X-Content-Type-Options:nosniffReferrer-Policy:same-originSet-Cookie:csrftoken=vppzMvcQcFpab9kFeNenX3cUVvOzaK59Cfa0JNXXQIpqk4Yw7;expires=2021年12月2日星期四02:41:39GMT;最大年龄=31449600;路径=/;SameSite=Lax,sessionid=7ngs826bws34mdjkbb6f60xsuikzjmi1;到期=2020年12月17日星期四02:41:39GMT;仅限HTTP;最大年龄=1209600;路径=/;SameSiteLax{"key":"1abc5ac07aab3395dfe4e832f7507250af4783a9"}(5)、登录用户操作创建视图,视图设置权??限为IsAuthenticatedfromrest_framework.viewsimportAPIViewfromrest_framework.responseimportResponsefromrest_framework.permissionsimportIsAuthenticatedclassUserDetailView(APIView):permission_classes=[IsAuthenticated,]defget(self,request,*args,**kwargs):({"email"se请求。user.email},status=200)user_detail_view=UserDetailView.as_view()添加路由fromdjango.contribimportadminfromdjango.urlsimportpath,include,re_pathfrom.viewsimport(user_detail_view)urlpatterns=[path('admin/',admin.site.urls),re_path(r'^auth/',include('rest_auth.urls')),re_path(r'^auth/registration/',include('rest_auth.registration.urls')),path('me/',user_detail_view)#获取登录用户的邮箱]发送请求###MeGEThttp://127.0.0.1:8000/me/HTTP/2.0Content-Type:application/jsonAuthorization:Token1abc5ac07aab3395dfe4e832f7507250af4783a9http请求中需要ContainsAuthorization,内容为Token登录后返回的key,如果不写tokenkeyHTTP/1.1401未经授权日期:2020年12月3日星期四02:50:18GMT服务器:WSGIServer/0.2CPython/3.7.0Content-Type:application/jsonWWW-Authenticate:TokenVary:AcceptAllow:GET,HEAD,OPTIONSX-Frame-Options:DENYContent-Length:58X-Content-Type-Options:nosniffReferrer-Policy:同源{"detail":"Authenticationcredentialswerenotprovided."}成功返回需要获取的内容相关介绍视频:JustDjango的dajngo-rest-auth
