rest_framework的下载

1
pip install djangorestframework

在settings中注册rest_framework

1
2
3
4
5
6
7
8
9
10
11
12
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'corsheaders', #后端跨域
'rest_framework', #rest_framework框架
'myapp',
'dwebsocket'
]

在django的app中创建一个serializers.py文件

1
2
3
4
5
6
7
8
9
10
11
12
13
#导包
from rest_framework import serializers

#导入需要序列化的表
from myapp.models import *


#建立序列化类
class CarouselSer(serializers.ModelSerializer):
#针对表进行序列化
class Meta:
model = Carousel #表名
fields = '__all__' #所有字段

在views视图中

1
2
3
4
5
6
7
8
9
from myapp.myser import *  #导入序列器

class GetCarousel(APIView):
def get(self,request):
#读库
carousels = Carousel.objects.all()
#序列化操作
carousels_ser = CarouselSer(carousels,many=True)
return Response(carousels_ser.data)

使用序列化的增删改查操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#url: path('getcarousel/',GetCarousel.as_view()),
class GetCarousel(APIView):
#查询操作
def get(self,request):
#读库
carousels = Carousel.objects.all()
#序列化操作
carousels_ser = CarouselSer(carousels,many=True)
return Response({'data':carousels_ser.data})

#添加操作
def post(self,request):
name = request.data.get('name',None)
src = request.data.get('link',None)
img = request.data.get('imgs',None)
ser = Carousel.objects.filter(name=name).first()
if ser:
return Response({'msg':'幻灯片已存在'})
carousel = Carousel(name=name,src=src,img=img)
carousel.save() #保存入库
return Response({'code': 200, 'msg': '添加成功'})

#删除操作
def delete(self,request):
id = request.GET.get('id',None)
Carousel.objects.filter(id=id).delete()
return Response({'code': 200, 'msg': '删除成功'})

#修改操作
def put(self,request):
id = request.data.get('id',None)
name = request.data.get('name',None)
src = request.data.get('link',None)
img = request.data.get('imgs',None)
car = Carousel.objects.filter(id=id).first()
car.name = name
car.src = src
car.img = img
car.save()
return Response({'code':200, 'msg': '修改成功'})

注意

1
2
3
4
5
rest_framework自带增删改查四种方法
查询:get:Carousel.objects.all() --->method:'GET'
增加:post:carousel.save() --->method:'POST(data)',request.data.get()
删除:delete:Carousel.objects.filter(id=id).delete() --->method:'DELETE(params)',request.GET.get()
修改:put:carousel.save() --->method:'PUT(data)',request.data.get()

vue代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<template>
<div>
<center>
<table border="1" style="text-align: center;">
<tr>
<td>
名称
</td>
<td>
链接
</td>
<td>
图片
</td>
<td>
添加
</td>
<td>
修改
</td>
<td>
删除
</td>
</tr>
<tr v-for="(item,index) in lunbo_list" :key="index">
<td>
&emsp;&emsp; {{item.name}} &emsp;&emsp;
</td>
<td>
&emsp;&emsp; <a :href="item.src" target="_black">{{item.src}}</a> &emsp;&emsp;
</td>
<td>
&emsp;&emsp; <img :src="item.img" alt=""> &emsp;&emsp;
</td>
<td>
&emsp;&emsp; <Button color="green" @click="put_lunbo(item.id)">修改</Button> &emsp;&emsp;
</td>
<td>
&emsp;&emsp; <Button color="red" @click="del_lunbo(item.id)">删除</Button> &emsp;&emsp;
</td>
</tr>
</table>
<br>
<table>
<tr>
<td>
name:<input type="text" v-model="name">
</td>
</tr>
<tr>
<td>
img:<input type="text" v-model="imgs">
</td>
</tr>
<tr>
<td>
src:<input type="text" v-model="link">
</td>
</tr>
<br>
<tr>
&emsp;&emsp; <Button color="blue" @click="add_lunbo">添加</Button> &emsp;&emsp;
</tr>
</table>
</table>
<br>
<table>
<tr>
<td>
name:<input type="text" v-model="name">
</td>
</tr>
<tr>
<td>
img:<input type="text" v-model="imgs">
</td>
</tr>
<tr>
<td>
src:<input type="text" v-model="link">
</td>
</tr>
<br>
<tr>
&emsp;&emsp; <Button color="blue" @click="put_lunbo1">修改</Button> &emsp;&emsp;
</tr>
</table>
</center>
</div>
</template>


<script>
export default {
data(){
return{
lunbo_list:[],
name:"",
link:"",
imgs:"",
}
},
mounted() {
this.lunbo();
},
methods: {
lunbo(){
this.axios({
url:'http://localhost:8000/getcarousel/',
method:'GET',
}).then(resp=>{
console.log(resp)
this.lunbo_list = resp.data.data
})
},
//增加
add_lunbo(){
this.axios({
url:'http://127.0.0.1:8000/getcarousel/',
method:'POST',
data:{
name:this.name,
link:this.link,
imgs:this.imgs,
}
}).then(resp=>{
console.log(resp)
this.$router.go(0)
})
},
//删除
del_lunbo(id){
this.axios({
url:'http://127.0.0.1:8000/getcarousel/',
method:'DELETE',
params:{
id:id
}
}).then(resp=>{
console.log(resp)
alert(resp.data.msg)
this.$router.go(0)
})
},
//修改跳转
put_lunbo(id){
//网页跳转传递id
this.$router.push({'path':'/my_lunbo',query:{'aid':id}})
},
//修改
put_lunbo1(){
this.axios({
url:'http://127.0.0.1:8000/getcarousel/',
method:'PUT',//指定修改方法
data:{
id:this.$route.query.aid,
name:this.name,
link:this.link,
imgs:this.imgs,
}
}).then(resp=>{
console.log(resp)
alert(resp.data.msg)
this.$router.go(0)
})
}
},
}
</script>


<style>

img{
width: 100px;
height: 100px;
}
</style>

评论





载入天数...载入时分秒...

Blog content follows the Attribution-NonCommercial-ShareAlike 4.0 International (CC BY-NC-SA 4.0) License

Use WZH as theme, total visits times