AI學習吧-登錄註冊
阿新 • • 發佈:2018-12-11
登錄註冊 efault 只需要 vue 技術分享 onetoone ice span 分享
如果需要給表設置權限,沒有登錄就不可以查看,只需要在每個視圖函數之前加上Auth_classes=[ ]即可!
增加兩張表,做登錄認證 #models.py #做登錄驗證 class Users(models.Model): user=models.CharField(max_length=32) pwd=models.CharField(max_length=32) type=((1,"VIP"),(2,"SVIP"),(3,"SSVIP")) user_type=models.IntegerField(choices=type)class Usertoken(models.Model): token=models.CharField(max_length=128) user=models.OneToOneField("Users")
url("login", LoginView.as_view()),
#app01/view/login.py from rest_framework.views import APIView from ..models import * from ..utils.response import BaseResponse fromrest_framework.response import Response class LoginView(APIView): """ 1000:成功 1001:用戶名或者密碼錯誤 1002:異常錯誤 """ def post(self, request): response=BaseResponse() #重寫了這個裝填碼的類 try: print(request.data) user = request.data.get("user") pwd= request.data.get("pwd") user = Users.objects.filter(user=user, pwd=pwd).first() import uuid random_str = uuid.uuid4() if user: Usertoken.objects.update_or_create(user=user, defaults={"token": random_str}) response.user = user.user response.token= random_str else: response.code= 1001 response.msg= "用戶名或者密碼錯誤" except Exception as e: response.code = 1002 response.msg= str(e) return Response(response)
#app01/utils/response.py class BaseResponse(object): def __init__(self,): self.code=1000 self.data="" self.error="" @property def dict(self): return self.__dict__ if __name__ == ‘__main__‘: response=BaseResponse() response.xxx=123 print(response.dict)
結合VUE即可做登錄驗證
AI學習吧-登錄註冊