drf--ViewSet (指定get关联的函数) 第一波

ViewSet 实现cbv模式指定请求对应函数,函数里面可以放get的多个方法等

urls.py

from django.urls import path,re_path
urlpatterns = [
    # ^ :匹配字符串开头,#$ :匹配字符串末尾,
    url('^bb/$', viewset.BookView2.as_view({"get":"get_all","post":"add_all"})),
    re_path("^bb/(?P<pk>.*)$", viewset.BookView2.as_view({"get":"add_all_parm"})),
]

 views.py

from django.http import HttpResponse,JsonResponse
from rest_framework.viewsets import ViewSet

class BookView2(ViewSet):
    # 查询数据
    def get_all(self,request):
        return HttpResponse("获取所有")

    # 添加数据
    def add_all(self, request):
        return HttpResponse("添加所有资源")

    # 添加数据
    def add_all_parm(self, request,pk):
        return HttpResponse("添加所有资源add_all_parm")

 

posted @ 2022-10-18 01:32  凯宾斯基  阅读(25)  评论(0编辑  收藏  举报