Django---drf请求和响应、视图---作业
1 自己封装一个CommonResponse对象,使用方法如下
return CoomonResponse('100','成功',boo_ser.data)
return CoomonResponse('101','验证失败',boo_ser.errors)
2 什么是正向代理,什么是反向代理
服务器A是别人的内网,服务器B可以访问,我们需要通过服务器B去访问服务器A成为正想代理
发布了一个项目在服务器A,但是不想让人知道自己ip怕有危险,浴室就让服务器B去访问服务器A,然后把A返回的信息给B,
你访问服务器A其实是在访问服务器B,这样就把A的地址隐藏了,称为反向代理。
3 大家都写:分别用APIView,GenericAPIView,GenericAPIView和5个视图扩展类写的接口和ModelViewSet写5个接口
views.py
from django.shortcuts import render
from app01 import models
# Create your views here.
from app01.ser import BookSerializer
from app01.models import Book
from rest_framework.views import APIView
from rest_framework.response import Response
#Apiview
class BookView(APIView):
def get(self,request):
book_list = Book.objects.all()
book_ser = BookSerializer(book_list,many=True)
return Response(book_ser.data)
def post(self,request):
book_ser = BookSerializer(data=request.data)
if book_ser.is_valid():
book_ser.save()
return Response(book_ser.data)
else:
return Response({'status':101,'msg':'校检失败'})
class BookDetailView(APIView):
def get(self,request,pk):
book = Book.objects.all().filter(pk= pk).first()
book_ser = BookSerializer(book)
return Response(book_ser.data)
def put(self,request,pk):
book = Book.objects.all().filter(pk=pk).first()
book_ser = BookSerializer(instance=book,data=request.data)
if book_ser.is_valid():
book_ser.save()
return Response(book_ser.data)
else:
return Response({'status':'101','msg':'校检失败'})
def delete(self,request,pk):
ret = Book.objects.filter(pk=pk).delete()
return Response({'status':100,'msg':'删除成功'})
#基于GennerricAPIview
from rest_framework.generics import GenericAPIView
class Book2View(GenericAPIView):
queryset = Book.objects
serializer_class = BookSerializer
def get(self,request):
book_list = self.get_queryset()
book_ser = BookSerializer(book_list,many=True)
return Response(book_ser.data)
def post(self,request):
book_ser = self.get_serializer(data=request.data)
if book_ser.is_valid():
book_ser.save()
return Response(book_ser.data)
else:
return Response({'status':'101','msg':'校检失败'})
class Book2DetailView(GenericAPIView):
queryset = Book.objects
serializer_class = BookSerializer
def get(self,request,pk):
book = self.get_object()
book_ser = self.get_serializer(book)
return Response(book_ser.data)
def put(self,request,pk):
book = self.get_object()
book_ser = self.get_serializer(instance=book,data=request.data)
if book_ser.is_valid():
book_ser.save()
return Response(book_ser.data)
else:
return Response({'status':'101','msg':'校检失败'})
def delete(self,request,pk):
ret = self.get_object().delete()
return Response({'status':100,'msg':'删除成功'})
#GennerricAPIView和5个视图扩展类
from rest_framework.mixins import ListModelMixin,CreateModelMixin,UpdateModelMixin,\
DestroyModelMixin ,RetrieveModelMixin
class Book3View(GenericAPIView,ListModelMixin,CreateModelMixin):
queryset = Book.objects
serializer_class = BookSerializer
def get(self,request):
return self.list(request)
def post(self,request):
return self.create(request)
class Book3DetailView(GenericAPIView,RetrieveModelMixin,DestroyModelMixin,UpdateModelMixin):
queryset = Book.objects
serializer_class = BookSerializer
def get(self,request,pk):
return self.retrieve(request,pk)
def put(self,request,pk):
return self.update(request,pk)
def delete(self,request,pk):
return self.delete(request,pk)
#ModelViewSet
from rest_framework.viewsets import ModelViewSet
class Book4View(ModelViewSet):
queryset = Book.objects
serializer_class = BookSerializer
models.py
from django.db import models
# Create your models here.
class Book(models.Model):
id = models.AutoField(primary_key=True)
name = models.CharField(max_length=32)
price = models.DecimalField(decimal_places=2,max_digits=5)
author = models.CharField(max_length=32)
publish = models.CharField(max_length=64)
app01下ser.py创建序列化类
ser.py
from rest_framework import serializers
from app01.models import Book
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = '__all__'
urls.py
from django.contrib import admin
from django.urls import path,re_path
from app01 import views
urlpatterns = [
path('admin/', admin.site.urls),
path('books/',views.BookView.as_view()),
re_path('book/(?P<pk>\d+)',views.BookDetailView.as_view()),
path('books2/', views.Book2View.as_view()),
re_path('book2/(?P<pk>\d+)', views.Book2DetailView.as_view()),
path('books3/', views.Book3View.as_view()),
re_path('book3/(?P<pk>\d+)', views.Book3DetailView.as_view()),
path('books4/', views.Book4View.as_view(actions={'get':'list','post':'create'})),
re_path('book4/(?P<pk>\d+)', views.Book4View.as_view(actions={'get':'retrieve','put':'update','delete':'destroy'})),
]
settings.py注册djangorestframwork组件
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'app01.apps.App01Config',
'rest_framework'
]
4 今天讲的所有视图类的继承关系,写出来
APIView>>>View>>>object>>>hashable
GenericAPIView>>>APIView>>>View>>>object>>>hashable
ListModelMixin,CreateModelMixin,UpdateModelMixin,DestroyModelMixin ,RetrieveModelMixin>>>object
ModelViewSet ListModelMixin,CreateModelMixin,UpdateModelMixin,DestroyModelMixin ,RetrieveModelMixin>>>object
GenericAPIView>>>APIView>>>View>>>object>>>hashable
GenericAPIView>>>ViewSetMixin