APIView与View区别:
重写as_view(): csrf_token豁免
重写dispatch():
封装原生request在Request类的对象里,
request.GET变成request.query_params
request.POST变成request.data(注意不是serializer.data)
request.META还是request.META(请求头数据dict)
views.py
from .models import Book # from .serializers import BookSerializer from .model_serializer import BookSerializer from rest_framework.viewsets import ModelViewSet class BookModelView(ModelViewSet): queryset = Book.objects.all() serializer_class = BookSerializer
urls.py
from django.urls import path from . import views app_name = 'serdemo' urlpatterns = [ path("book/list", views.BookModelView.as_view(actions={"get": "list", "post": "create"})), path("retrieve/<int:pk>", views.BookModelView.as_view(actions={"get": "retrieve", "put": "update", "delete": "destroy"})) ]