urls:

from django.conf.urls import url
from app01 import views urlpatterns = [
# url(r"comment/$",views.Comment.as_view()),
url(r"comment/(?P<pk>\d+)",views.CommentDetailView.as_view()),
url(r"article/$",views.Article.as_view()),
url(r"school/(?P<id>\d+)",views.SchoolDetail.as_view(),name="school-detail"),
url(r"school_view/$",views.SchoolView.as_view(),name="school_view-detail"),
url(r"comment_view/$",views.CommentView.as_view(),name="comment_view-detail"),
url(r"article/(?P<pk>\d+)",views.ArticleDetail.as_view(),name="article-detail"), # url(r'comment/$',views.CommentViewSet.as_view({
# "get":"list",
# "put":"create",
# })),
#
# url(r'comment/(?P<pk>\d+)/$',views.CommentViewSet.as_view({
# "get":"retrieve",
# "put":"update",
# "delete":"destroy"
# })) ] from rest_framework.routers import DefaultRouter router = DefaultRouter()
router.register(r'comment',views.CommentViewSet)
urlpatterns += router.urls # 相当于 在urlpatterns里添加了:
# url(r'comment/$',views.CommentViewSet.as_view({
# "get":"list",
# "put":"create",
# })),
#
# url(r'comment/(?P<pk>\d+)/$',views.CommentViewSet.as_view({
# "get":"retrieve",
# "put":"update",
# "delete":"destroy"
# }))

views:此处为RESTful API 终极版:

from django.shortcuts import render,HttpResponse
from django.http import JsonResponse
from app01 import models
from rest_framework import serializers
from django import views
from rest_framework.views import APIView
from rest_framework.response import Response
from app01 import serializers as app01_serializers
from rest_framework.generics import GenericAPIView
from rest_framework.mixins import ListModelMixin,CreateModelMixin,RetrieveModelMixin,UpdateModelMixin,DestroyModelMixin
from rest_framework.generics import GenericAPIView, RetrieveUpdateDestroyAPIView, ListCreateAPIView
from rest_framework.viewsets import ModelViewSet # ==================================================================
# 95天内容 # 第一种方式: # class CommentView(APIView):
# def get(self,request):
# query_set = models.Comment.objects.all()
# ser_obj = app01_serializers.CommentSerializer(query_set,many=True)
# return Response(ser_obj.data) # 使用封装类后: # class CommentView(GenericAPIView,ListModelMixin,CreateModelMixin):
"""
GenericAPIView
只需要配置
queryset 和 serializer_class 即可
""" # queryset = models.Comment.objects.all() # 指定queryset对象
# serializer_class = app01_serializers.CommentSerializer # 指定类序列化
#
# def get(self,request, *args, **kwargs):
# return self.list(request, *args, **kwargs) # list方法为 ListModelMixin内部封装的函数,其源码为:
# def list(self, request, *args, **kwargs):
# queryset = self.filter_queryset(self.get_queryset())
#
# page = self.paginate_queryset(queryset)
# if page is not None:
# serializer = self.get_serializer(page, many=True)
# return self.get_paginated_response(serializer.data)
#
# serializer = self.get_serializer(queryset, many=True)
# return Response(serializer.data)
# 其实质与之前我们使用APIView一样,
# post 同理 # def post(self,request, *args, **kwargs):
# print("评论区")
# return self.create(request, *args, **kwargs) # 评论详情: # class CommentDetailView(GenericAPIView,RetrieveModelMixin,DestroyModelMixin,UpdateModelMixin):
# queryset = models.Comment.objects.all()
# serializer_class = app01_serializers.CommentSerializer
#
# def get(self,request,pk):
# return self.retrieve(request,pk)
#
# def delete(self,request,pk):
# return self.destroy(request,pk)
#
# def perform_destroy(self, instance):
# "someting code"
# # 可以在删除之前做的一些操作
# instance.delete()
#
# def put(self,request,pk):
# return self.update(request,pk) class SchoolView(GenericAPIView,ListModelMixin):
queryset = models.Comment.objects.all()
serializer_class = app01_serializers.CommentSerializer def get(self,request,*args,**kwargs):
return self.list(request, *args,**kwargs) # 第二种方式
class CommentDetailView(RetrieveUpdateDestroyAPIView): # RetrieveUpdateDestroyAPIView 相当于封装了GenericAPIView
# RetrieveModelMixin,DestroyModelMixin,UpdateModelMixin 这四个
# 可以进行查看,更改和删除操作。
queryset = models.Comment.objects.all()
serializer_class = app01_serializers.CommentSerializer #
class CommentView(ListCreateAPIView):
# ListCreateAPIView:内封装了 ListModelMixin CreateModelMixin
queryset =models.Comment.objects.all()
serializer_class = app01_serializers.CommentSerializer # 第三种方式:
# 终极版:
class CommentViewSet(ModelViewSet):
queryset = models.Comment.objects.all()
serializer_class = app01_serializers.CommentSerializer
05-11 17:02