1. 程式人生 > >Django - DRF - URL路由控制

Django - DRF - URL路由控制

目錄

一、原始方式

二、ViewSetMixin - 重寫as_view,攔截分發至指定方法

2-1 路由設計

2-2 檢視函式

三、routers自動生成路由(不推薦使用)

3-2 路由設計


一、原始方式

from django.conf.urls import url
from app01 import views
urlpatterns = [
    url(r'^publish/$', views.PublishView.as_view()),
    url(r'^publish/(?P<pk>\d+)', views.PublishDetailView.as_view()),
]


'''
檢視函式
'''

# -----------基於restful的增刪改  --------------
class PublishView(APIView):
    def get(self, request):
        publish_list = models.Publish.objects.all()
        ser = MySerializer.PublishSerializers(publish_list, many=True)
        return JsonResponse(ser.data, safe=False)

    def post(self, request):
        ser = MySerializer.PublishSerializers(data=request.data)
        if ser.is_valid():
            ser.save()
            return JsonResponse(ser.data, safe=False)
        else:
            return JsonResponse(ser.errors, safe=False)


class PublishDetailView(APIView):
    def get(self, request, pk):
        publish_obj = models.Publish.objects.filter(pk=pk).first()
        ser = MySerializer.PublishSerializers(publish_obj, many=False)
        return JsonResponse(ser.data, safe=False)

    def put(self, request, pk):
        publish_obj = models.Publish.objects.filter(pk=pk).first()
        ser = MySerializer.PublishSerializers(data=request.data, instance=publish_obj)
        if ser.is_valid():
            ser.save()
            return JsonResponse(ser.data, safe=False)
        else:
            return JsonResponse(ser.errors, safe=False)

    def delete(self, request, pk):
        models.Publish.objects.filter(pk=pk).delete()
        return JsonResponse("delete", safe=False)

二、ViewSetMixin - 重寫as_view,攔截分發至指定方法

2-1 路由設計

from django.conf.urls import url
from django.contrib import admin
from app01 import views
urlpatterns = [
    # 基於ViewSetMixin
    url(r'^publish/$', views.PublishView.as_view({'get': 'list', 'post': 'create'})),
    url(r'^publish/(?P<pk>\d+)', views.PublishView.as_view({'get': 'retrieve', 'put': 'update','delete':'destroy'})),

]
'''
PublishView類內包含list、create、retrieve、update、destory等方法

get請求-list方法,post請求-create方法
.PublishView.as_view({'get': 'list', 'post': 'create'})

get請求-retrieve方法,put請求-update方法,delete請求-destory方法
.PublishView.as_view({'get': 'retrieve', 'put': 'update','delete':'destroy'})
'''



from django.conf.urls import url,include
from django.contrib import admin
from app01 import views

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^test1/$', views.TestAll.as_view({'get': 'test'})),
    url(r'^test2/$', views.TestAll.as_view({'get': 'test2'})),
    url(r'^test3/$', views.TestAll.as_view({'get': 'test3'})),

    url('', include(router.urls)),

]
'''
test1 - 觸發test方法
test2 - 觸發test2方法
test3 - 觸發test3方法

'''

2-2 檢視函式

'''
!!注意!!由於viewSetMixin重寫了as_view方法和APIView內衝突,所以繼承時必須前置
'''
class PublishView(ViewSetMixin,APIView):

    def list(self, request, *args, **kwargs):
        pass

    def create(self, request, *args, **kwargs):
        pass

    def retrieve(self, request, *args, **kwargs):
        pass

    def update(self, request, *args, **kwargs):
        pass

    def destroy(self, request, *args, **kwargs):
        pass

三、routers自動生成路由(不推薦使用)

3-2 路由設計

from django.conf.urls import url,include
from django.contrib import admin
from app01 import views


from rest_framework import routers
# 生成一個router物件
router=routers.DefaultRouter()
# 需要傳兩個引數,第一個引數就是匹配的路徑,第二個引數,是檢視類
# publish匹配PublishView檢視函式
router.register('publish',views.PublishView)
# !!!注意!!!PublishView方法必須繼承於ModelViewSet

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url('', include(router.urls)),


]