首页 > 其他分享 >drf--ViewSet

drf--ViewSet

时间:2022-10-18 01:44:18浏览次数:48  
标签:BookView2 return get -- add HttpResponse ViewSet drf

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<id>.*)$", 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,id):
        return HttpResponse("添加所有资源add_all_parm")

 

标签:BookView2,return,get,--,add,HttpResponse,ViewSet,drf
From: https://www.cnblogs.com/kaibindirver/p/16801258.html

相关文章