创建精写报错信息表用于记录错误信息(新的文件夹,创建新的py)
#静写报错信息class BaseResponse(object): def __init__(self): self.code=1000 #状态 self.error='' self.data='' @property #变成属性 可以不加() 直接调用 def dict(self): return self.__dict__
创建精写报错信息表用于记录错误信息(新的文件夹,创建新的py)
from rest_framework.views import APIViewfrom login.serializers import AccountSerializersfrom rest_framework.response import Responsefrom utils.base_response import BaseResponse ##精写报错信息from course.models import Accountimport uuid #注册class Register(APIView): # 用户注册传用户名和密码 # 相当于给用户表新增数据 def post(self,request): # 获取用户名和密码 # 拿序列化器做验证 ser_obj=AccountSerializers(data=request.data) if ser_obj.is_valid(): ser_obj.save() return Response('注册成功') #登录class LoginView(APIView): def post(self,request): ret=BaseResponse() #实例化对象 #获取用户和密码 username=request.data.get('username','') if not username: ret.code=1010 ret.error='用户名不能为空' return Response(ret.dict) pwd=request.data.get('pwd','') if not pwd: ret.code=1011 ret.error='密码不能为空' return Response(ret.dict) # 判断是否有这个用户对象 try: user_obj=Account.objects.filter(username=username,pwd=pwd).first() if not user_obj: ret.code=1012 ret.error='用户名和密码错误' return Response(ret.dict) user_obj.token=uuid.uuid4() user_obj.save() ret.data='登录成功' except Exception as e: ret.code=1013 ret.error='登录失败' return Response(ret.dict)