图书详情页面的编辑
前端
<template> <div> <p>用户:<input type="text" v-model="name" disabled></p> <p>年龄:<input type="text" v-model="age" ></p> <p>地址:<input type="text" v-model="address" ></p> <p>身高:<input type="text" v-model="height" ></p> <p>密码:<input type="text" v-model="pwd" ></p> <p> 班级:<input type="text" v-model="room" disabled> <!-- <select v-model="room_name"> <option v-for="i in room" :value="i.id"></option> </select> --> </p> <!-- <h1></h1> --> <p><input type="submit" @click="uppeople()"></p> </div> </template> <script> import { drf_onepeople_get,drf_onepeople_put} from '../../axios_send/api' export default { name:"drf_peopleup", data() { return { name:'', age:'', address:'', height:'', pwd:'', room:'' } }, methods: { get_people(){ var id = localStorage.getItem("people_id") alert(id) drf_onepeople_get(id) .then(res=>{ console.log(res) if(res.code == 200){ this.name = res.data.name, this.pwd = res.data.password, this.height = res.data.height, this.age = res.data.age, this.address = res.data.address, this.room = res.data.room.name } }) }, }, created(){ this.get_people() } } </script>
后端
class Peoples(APIView): def get(self,request): data = request.query_params id = data["id"] print(id) try: room = Room.objects.get(id=id) print(room) serializer = RoomSerializer(room) data = serializer.data res_data = { "code":200, 'msg':"seuucess", "data":data } return Response(data=res_data,status=200) except Exception as e: # raise e res_data = { "code": 500, 'msg': "fail", } return Response(data=res_data,status=200) def post(self,request): data = request.data try: #序列化器做反序列化使用 serializer = PeopleSerializer(data=data) #反序列化时候的验证 serializer.is_valid() print(serializer.errors) #数据保存 serializer.save() #获取最后的结果 res_data = { "code": 200, "msg": "success", } return Response(data=res_data, status=200) except Exception as e: # raise e res_data = { "code": 500, "msg": "fail", } return Response(data=res_data,status=200) def put(self,request): data = request.data print(data) try: rooms = Room.objects.get(name=data["room"]) id = rooms.id data["room"] =id people = People.objects.get(name=data["name"]) serializer = PeopleSerializer(people,data=data) serializer.is_valid() serializer.save() return Response(data={"code":200,"msg":"success"},status=200) except: return Response(data={"code":500,"msg":"fail"}, status=200) def delete(self,request): pass