Django rest_framework source code analysis,

Source: Internet
Author: User

Django rest_framework source code analysis,

Before reading the source code, let's take a look at what is rest and restful api.

What is rest?

To sum up, REST is the guiding principle of architecture design that all Web applications should abide. REST is short for Representational State Transfer ".

Restful api

API that complies with the REST architecture design.

Restful api writing specifications:

-URL (it is shown as an api in the url)
-Url noun (url should be a noun)
-Version (indicates the version number in the url)
-Submission method (the most important method is to determine the operation to be performed based on the submission method)
-Status (returned status code)
-Hypermedia link (return link in api)
-Error details (when the status code is 4xx, the error message should be returned, and the error is used as the key .)

Next let's take a look at the process in its source code.

 

UserInfo class:

from django.shortcuts import render,HttpResponsefrom rest_framework.views import APIViewfrom rest_framework.exceptions import APIExceptionclass UserInfo(APIView):    authentication_classes=[Myauthentication,]    def get(self,request):        return HttpResponse(".....")

First, the dispathch () method in the corresponding class will be executed when the request comes in. If the current class does not exist, find it in its parent class APIView.

In step 2, it calls initialize_request and assigns its return value to the request. It means that the current request is not the original request of django, And then we click in it to do something.

In the initialize_request method, the request object is re-encapsulated and some things are re-encapsulated based on the original one. Now let's take a look at what get_authenticators () has done.

 

In the get_authenticators method, the self. authentication_classes loop returns a list object, because it is found by self., all of which go to the class we wrote to find authentication_classes. We

The written class is not found in the parent class, so we can customize this list class. Before defining it, you need to know what is in its default class, so that we can define it according to its default class.

It can be seen that the default authentication_classes is read from its configuration file. Now you can check the classes in its configuration file.

Now you can check the methods in both classes.

 

We can see that they all inherit a BaseAuthentication class and all implement the authenticate Method. So now let's see what the BaseAuthentication class is?

 

It can be seen that the BaseAuthentication class is actually an interface class, so that the class that inherits it must implement the authenticate Method.

Finally, some authentication objects are encapsulated in authenticatotes in the request object. Then, let's look at them,

Then execute the initial method.

Then, authenticate the method in request. user for authentication.

This is the general process of authentication. If all other permissions are the same, we will not explain them one by one.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.