test development python django-73.django view CBV and FBV

Foreword

FBV (function base views) is to use the function to process the request in view, the way it usually is time to start learning django Getting used.
CBV (class base views) is used in the view request handling, which is an object-oriented programming.

When the interview basically asked: Do you usually write view is the view function (FBV) based, or class-based view (CBV), in which the difference between the two?
If you just write view functions based on that description still in the initial stage of the entry.

FBV mode

FBV (function base views) view function is defined in the file views.py user requests are processed, if the function is determined by way request.method request is GET or POST request to make corresponding process.

# views.py
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse
# 上海悠悠,QQ交流群:750815713
# Create your views here.

# function base views

def fbvdemo(request):
    '''function base views'''
    context = {
        "code": 0,
        "msg": ""
    }
    if request.method == "GET":
        context["msg"] = "这是fbvdemo get请求"
        return JsonResponse(context)

    if request.method == "POST":
        context["msg"] = "这是fbvdemo POST请求"
        return JsonResponse(context)

urls.py configure access path

from django.conf.urls import url

urlpatterns = [

    url(r'^fbvdemo/$', views.fbvdemo)
]

CBV mode

CBV (class base views) is used in the view request handling

  • Custom View class must inherit the parent class
  • Improve the reusability of code, object-oriented techniques may be used, such as Mixin (multiple inheritance)
  • May be processed for various different functions HTTP method, in many if not determining, improve code readability
  • CBV View mode inherits class provides various functions in response to the request of the unwanted mode determination is performed, you can direct overwrite methods inherited subclass
  • CBV mode subclass inherits the parent request response function overrides embodiment reflected by the distribution method of the parent class dispatch
  • You must be used in Myview.as_view urls.py routing system () function alternate view
# views.py
from django.shortcuts import render
from django.http import HttpResponse, JsonResponse

# Create your views here.
# 上海悠悠,QQ交流群:750815713
# class base views

class Cbvdemo(View):
    context = {
        "code": 0,
        "msg": ""
    }

    def get(self, request):
        self.context["msg"] = "这是Cbvdemo get请求"
        return JsonResponse(self.context)

    def post(self, request):
        self.context["msg"] = "这是Cbvdemo post请求"
        return JsonResponse(self.context)

urls.py configure access path

from django.conf.urls import url

urlpatterns = [

    url(r'^fbvdemo/$', views.fbvdemo),
    url(r'^cbvdemo/$', views.Cbvdemo.as_view()),
]

These two ways Functionally corresponding function can be achieved, from the readability of the code is concerned, it is recommended to use CBV mode.
Also by CBV If you want to perform additional steps before executing the get or post method, you can override the dispatch.

Guess you like

Origin www.cnblogs.com/yoyoketang/p/11917734.html