首先咱們若是用傳統方式在url裏面定義路由,在view裏面處理函數,好比訂單類,一張表增刪改查須要四個url對應對應四個def函數,若是需求多(多張表,如10張,40個url,40個函數),咱們豈不是要累死 。restful
所以出現兩種CBV和FBV函數
CBV :在views裏面定義類,而後列舉四個函數,處理。post
FBV:在views裏面定義函數,用if對請求進行判斷url
運行:rest
代碼實現:blog
a. 接口開發 urlpatterns = [ # url(r'^admin/', admin.site.urls), url(r'^get_order/', views.get_order), url(r'^add_order/', views.add_order), url(r'^del_order/', views.del_order), url(r'^update_order/', views.update_order), ] def get_order(request): return HttpResponse('') def add_order(request): return HttpResponse('') def del_order(request): return HttpResponse('') def update_order(request): return HttpResponse('') b. restful 規範(建議) 1. 根據method不一樣作不一樣的操做,示例: 基於FBV: urlpatterns = [ url(r'^order/', views.order), ] def order(request): if request.method == 'GET': return HttpResponse('獲取訂單') elif request.method == 'POST': return HttpResponse('建立訂單') elif request.method == 'PUT': return HttpResponse('更新訂單') elif request.method == 'DELETE': return HttpResponse('刪除訂單') 基於CBV: urlpatterns = [ url(r'^order/', views.OrderView.as_view()), ] class OrderView(View): def get(self,request,*args,**kwargs): return HttpResponse('獲取訂單') def post(self,request,*args,**kwargs): return HttpResponse('建立訂單') def put(self,request,*args,**kwargs): return HttpResponse('更新訂單') def delete(self,request,*args,**kwargs): return HttpResponse('刪除訂單')