1. 程式人生 > >AI學習吧-登入註冊

AI學習吧-登入註冊

 如果需要給表設定許可權,沒有登入就不可以檢視,只需要在每個檢視函式之前加上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
from rest_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即可做登入驗證