Day 21 drf introduction and application installation

Day 21 drf introduction and application installation

1. What is DRF

Django RESF framework is an open source Django extension that provides a convenient REST API development framework with the following features:

  • Intuitive API web interface.
  • Support for multiple authentication and authority authentication methods.
  • Built-in support for OAuth1 and OAuth2
  • Built-in current limiting system
  • Automatic serialization according to Django ORM or other libraries
  • Rich customization levels: function view, class view, view collection to automatically generated API to meet various needs
  • Extensibility, rich plug-ins
  • Widely used, rich documentation

Two, web development model

1. Front-end mixed development

(Template language: DTL), deal with many front-end problems

2. Front and rear ends are separated

The front end is a project, the back end is a project, joint debugging

Full stack development

3. Front end:

Not only refers to the web front end, mobile terminal, small program

  • -web: vue, react, html+css+jq
    -mobile terminal: ios (object c, swift), Android (java, Kotlin)-small
    program: WeChat encapsulates some of js, html, css

  • -Development to the present: big front-end concept -flutter: Dart
    -uni-app:vue framework

4. The backend only needs to return the json format string

Three, API interface

  • 1 /books/—>Return json format data—>/books/ is an api interface
  • 2 Open api interface
    -Weibo
    -Baidu
  • 3 api documentation (open for front-end viewing)
  • 4 Typical api interface-
    https://api.weibo.com/2/statuses/home_timeline.json

Fourth, the use of postman

  • After opening the backend, use postman to test the interface (api interface)
  • Use postman to export and import the test interface (go back and try it yourself)
  • Company test platform

Five, the installation and use of drf

  • djangorestframework: django app, can only be used on django
  • pip3 install djangorestframework
  • Simple to use, look at the code
    django: 2.0.7, version 1 is also available
    djangorestframework: 3.12.1

Six, CBV source code review

# drf:APIView的源码分析

    def as_view(cls, **initkwargs):
        # 这句话执行完成返回 view闭包函数的内存地址
        view = super().as_view(**initkwargs) # 调用父类(View)的as_view
        view.cls = cls
        view.initkwargs = initkwargs
        view=csrf_exempt(view)  # 局部禁用csrf
        return view

 # 装饰器的使用方式
@csrf_exempt   ====>view=csrf_exempt(view)
def view():
    pass

# 请求来了,会执行上面返回的view()---->self.dispatch(APIView的dispatch)


# APIView的dispatch方法
def dispatch(self, request, *args, **kwargs):
	# 把原生的request,封装进新的Request对象(drf的Request)
    request = self.initialize_request(request, *args, **kwargs)
    self.request = request
    
        try:
    	# 重点(频率,认证,权限。。。)
        self.initial(request, *args, **kwargs)

        if request.method.lower() in self.http_method_names:
            handler = getattr(self, request.method.lower(),
                              self.http_method_not_allowed)
        else:
            handler = self.http_method_not_allowed
		# 这个request新的requst,是drf中Request对象
        # response是原生response
        response = handler(request, *args, **kwargs)

    except Exception as exc:
        # 全局异常
        response = self.handle_exception(exc)
	# 把原生response包装了一下
    self.response = self.finalize_response(request, response, *args, **kwargs)
    return self.response

Guess you like

Origin blog.csdn.net/A1L__/article/details/109485070