What the hell is Django-rest-framework?

Author: HelloGitHub- Dream figure

Let's first review the traditional Django development workflow based on the template engine:

  1. Bind URL and view functions. When the user visits a URL, the bound view function is called for processing.
  2. Write the logic of the view function. Views usually involve database operations.
  3. Render the HTML template in the view and return an HTTP response.

In fact, the process of developing RESTful API based on django-rest-framework is completely similar:

  1. Bind URL and view functions. When the user visits a URL, the bound view function is called for processing.
  2. Write the logic of the view function, and perform corresponding operations on the requested resources according to the HTTP request type. This process usually involves the operation of the database.
  3. Use the agreed resource description format (such as XML or JSON) to serialize the resource and return the data to the client (via HTTP response).

The comparison shows that the first two steps are almost identical. The difference is that in the traditional template engine-based development method, resources are described using HTML documents and returned to the client, while in the RESTful API development method, resources are usually described as JSON or XML format and returned to the client .

Some students will ask, although django's view function usually returns the response of the HTML document, but django also supports returning the response in XML format or JSON format, so why use django-rest-framework?

In fact, it is indeed possible to return data in JSON or XML format in django, but the django framework itself only provides very basic functions. django-rest-framework is an extension based on django, specially designed for the development of RESTful API, and provides a very rich set of auxiliary classes and functions to help us develop the API conveniently. Let's briefly introduce what functional features django-rest-framework provides for us, and we will further study their usage in the next actual combat. Here we can first make a simple understanding from the macro level.

  • Content Negotiation. As mentioned before, in the RESFful architecture system, resources are passed between the client and the server in a certain description form, django-rest-framework automatically uses the appropriate resource description tool according to the resource format that the client can accept, and returns to the client Acceptable resources.
  • Authentication and authorization (Authentication and Permission). The client's operation of resources is usually limited, and some resources can only be operated by users who have been authenticated or have corresponding permissions. Django-rest-framework provides a wealth of authentication and authentication classes to help us Identity and permission verification.
  • Serialization (Serialization). Django is developed based on the Python language, so resources are usually described by Python objects, so when they are passed to the client, they must be converted, such as converting Python objects to JSON strings. This process is called serialization. The built-in serializer of django has limited functions. django-rest-framework provides a more powerful and powerful serializer, which makes the serialization of resources extremely simple.
  • Various generic views (Generic Views). Django provides various common view functions for the common processing logic in Web development to improve code reusability and reduce developer workload. django-rest-framework also provides various common view functions for the common processing logic in RESTful API development.
  • Automatic route generator (Router). django-rest-framework automatically generates URL routes that conform to the RESTful design according to the written view function.
  • Documentation. django-rest-framework automatically generates API documents based on the OpenAPI model, without our manual writing and maintenance.

In addition, django-rest-framework also provides various functional classes such as pagination, API versioning, caching, and throttling.

In the next practical tutorial, we will learn and use them one by one.

Let us officially start the learning journey of django-rest-framework!


Follow the public account to join the exchange group

Guess you like

Origin www.cnblogs.com/xueweihan/p/12706064.html