Introduction and development framework related to knowledge

Development Framework Introduction

First, the benefits of using the front and rear ends of separate development

  1. Back-end code and databases are not only at the front and rear ends of the separation of state for the front-end web development to provide data to support, but now, with the diverse needs app, small procedures, requirements and back-end data provides support for a variety of needs, before and after end of the separation, the rear end of the interface is provided to transfer data through the interface, can be adapted to different needs of development, greatly reduce the workload of the rear end of the development.
  2. Before and after the end of the separation allows developers to maintain the program more convenient.

Two, FBV and CBV

  • FBV: function base view based on the view function
    '' 'python

    def api1(request):
      data_list = ['课程', '班级']
      return HttpResponse(json.dumps(data_list))

'''

  • CBV: class base view based on the view class, is performed based on the reflected achieve different results depending on the embodiment of the request
    • principle:
      • Routing: Each route corresponding views of a custom class
      • View class: after receiving the request, the encapsulated directly call the view class dispatch scheduling method (the method to perform other reflection: the GET / the PUT / the PATCH / the PUT / the DELETE ......)
        '' 'Python

          class StudentView(View):
              def dispath(self, request, *args, **kwargs):
                  method_name = getattr(self, request.method.lower())
                  # 注意:这里的getattr()函数用于返回一个对象的属性值
                  # request.method返回的是请求的方式,即大写形式,如果我们
                  # 直接就把大写方式名作为执行函数的名字,系统会找不到,所有需要把
                  # 大写全部变成小写才可以找到并运行
                  result = method_name(request, *args, **kwargs)
                  return result
              def get(self, reqeust, *args, **kwargs):
                  return HttpResponse('GET')
        
              def post(self, reqeust, *args, **kwargs):
                  return HttpResponse('POST')
        
              def patch(self, reqeust, *args, **kwargs):
                  return HttpResponse('PATCH')
        
              def put(self, request, *args, **kwargs):
                  return HttpResponse('PUT')
        
              def delete(self, request, *args, **kwargs):
                  return HttpResponse('DELETE')
      '''
    • Process performed: after the view class mode, initiates the current request, view class may call upon receiving the request directly packaged dispatch methods , and returns the result to the front end, as our own custom classes in a class view dispatch methods, custom function will override the packaged method dispatch, case, initiate a request again after the front end, the view class will call from dispatch methods defined , return result, developers do not need to write the default case where the dispatch method.

'''python

# 由于每一个视图类都要调用dispatch方法,我们可以把它抽象出来,封装成一个基类,由视图函数来继承
class BaseView(object):
    def dispatch(self, request, *args, **kwargs):
        print("before")
        result = super(BaseView, self).dispatch(request, *args, **kwargs)
        # 基类继承自框架封装好的类并调用类中方法
        print("after")
        return result

class Student1View(BaseView, View):
    '''
    当继承多个类的时候,需要用到多继承关系,多继承关系遵循左优先原则,
    希望先继承的类写在左边,一次往下写
    '''
    def get(self, reqeust, *args, **kwargs):
        return HttpResponse('GET')

    def post(self, reqeust, *args, **kwargs):
        return HttpResponse('POST')
    
    def patch(self, request, *args, **kwargs):
        return HttpResponse('PATCH')

    def put(self, request, *args, **kwargs):
        return HttpResponse('PUT')

    def delete(self, request, *args, **kwargs):
        return HttpResponse('DELETE')

'''

Third, an object oriented encapsulation of

  • The same type of method Packaging category class, CBV format e.g.
  • The data is encapsulated into an object, such as when creating the class in the init

'''python

class Test(object):
    def __init__(self, a1,a2):
        self.a1 = a1
        self.a2 = a2
    def ...
    def ...
    
test1 = Test(111, 3333)
# 类的实例化

'''

Fourth, the middleware

Middleware five categories
  1. process_request
  2. process_view
  3. process_response
  4. process_exception
  5. process_render_template
csrf_token use

csrf_token middleware one, placed in processs_view, i.e. view function

  • csrf global settings set in the authentication, if desired a small part of the view from csrf certification function, may be added {% csrf_token%} form or a form in view add a decorative function: @csrf_exempt 'from django.views.dacorators.csrf import csef_exempt ', but in CBV mode, you must be exempt from certification decoration applied to the dispatch method , adding a separate view function is invalid, specifically three ways:
    • One way: decorator @csrf_exempt added directly before dispatch method
    • Second way: The method is performed by using decorative
      • 导入from django.utils.decorators import method_decorator
      • @method_decorator (csrf_exempt), add this to the front dispatch method decorator
    • Three ways: @method_decorator (csrf_exempt, name = 'dispatch'), name is a name of the method is to add decorative, ornamental front view class is added. The first two must find a way to add dispatch method, this no longer find the dispatch method, this method is more convenient.

      "Method_decorator Format: @method_decorator (csrf method, name = 'Add csrf view class method names')"

'''python

    class BaseView(object):
        @csrf_exempt
        def dispatch(self, request, *args, **kwargs):
            print("before")
            result = super(BaseView, self).dispatch(request, *args, **kwargs)
            print("after")
            return result

'''

  • If you do not set the global csrf certification, we hope for a small part of the view function csrf authentication, add a decorator before requiring authentication function @csrf_protect

Guess you like

Origin www.cnblogs.com/ddzc/p/12115912.html
Recommended