views.py APIView视图
from django.shortcuts import render
from rest_framework.views import APIView
from .models import AccountInfo
from .serializers import AccountInfoSerializer, AccountInfoDetailSerializer
from rest_framework.response import Response
from rest_framework.status import HTTP_201_CREATED,HTTP_200_OK,HTTP_204_NO_CONTENT
# Create your views here.
class AccountInfoListAPIView(APIView):
# 查询列表
def get(self,request):
accounts = AccountInfo.objects.all()
print(accounts)
ret_data = AccountInfoSerializer(accounts,many=True)
print(ret_data)
return Response(ret_data.data)
def post(self,request):
accountinfo = request.data
print(accountinfo)
verify_data = AccountInfoDetailSerializer(data=request.data)
if verify_data.is_valid():
accountinfo = verify_data.save()
return Response(verify_data.data,status=HTTP_201_CREATED)
else:
return Response(verify_data.errors)
class AccountInfoDetailAPIView(APIView):
'''针对单条数据的查,改,删'''
#通过传来的pk进行数据库内容查找
def get(self,request,pk):
print(pk)
try:
account = AccountInfo.objects.get(id=pk)
print("-->"*5,account.nick_name)
except AccountInfo.DoesNotExist:
return Response({'status_code':"404",'massage':'当前数据已失效,请您校对后重新查找!'})
# return Response(status=HTTP_204_NO_CONTENT)
ret_data = AccountInfoDetailSerializer(instance=account)
return Response(ret_data.data,status=HTTP_200_OK)
def put():
pass
def delete():
pass