from django.conf.urls import url from app01 import views urlpatterns = [ url(r'^books/$', views.BookView.as_view()), url(r'^books/(?P<pk>d+)$', views.BookDetailView.as_view()), ]
from rest_framework.viewsets import ViewSetMixin class ttt(ViewSetMixin,APIView):#继承关系,必须先继承ViewSetMixin函数,再继承APIView函数 #为的是在使用ViewSetMixin中重写的as_view函数 def aaa(self,request): return Response('aaaa') def bbb(self,request): return Response('bbbbbbb') def ccc(self,request): return Response('cccccccc')
那么在路由中就可以怎么书写
url(r'^ttt/', views.ttt.as_view({'get':'aaa','post':'bbb'})), url(r'^ggg/',views.ttt.as_view({'get':'ccc',})) #这样的方式,就可以使得一个类可以支持很多函数
from rest_framework.viewsets import ModelViewSet class PublishView(ModelViewSet): queryset = models.Publish.objects.all() serializer_class = PublishSer
同时需要在路由中配置
from django.conf.urls import include #include 路由分发 from rest_framework import routers router = routers.DefaultRouter() #生成对象 router.register('publish',views.PublishView) #配置视图层,和路由名称
在路由中的使用
url(r'', include(router.urls)),