How to see the source code of Django and DRF
Principle of CBV
class OrderView(View):
def get(self, request, *args, **kwargs):
ret = {
'code': 1000,
'msg': 'xxx'
}
return HttpResponse(json.dumps(ret), status=201)
def post(self, request, *args, **kwargs):
return HttpResponse('创建订单')
def put(self, request, *args, **kwargs):
return HttpResponse('更新订单')
def delete(self, request, *args, **kwargs):
return HttpResponse('删除订单')
-
We customize a
OrderView
class that inherits fromView
the class -
path('order/',views.OrderView.as_view()),
-
The method called in the url , but we did not define it, indicating that this method is inherited
OrderView
fromas_view()
View
-
so found
View
inas_view()
-
Which
cls
refers to our customOrderView
, butself
is the object after instantiation -
Next jump to
dispatch
method -
You can know `If you use the CBV mode in the Django project, you actually call getattr to execute the function corresponding to the request method in the acquisition class
-
Of course, you can also customize
dispatch
methods to implement your own operations.
DRF's APIView source code + user authentication
class Myauthentication(object):
# 用户认证
def authenticate(self, request):
token = request._request.GET.get('token')
# 此处可以获取用户名和密码,进行数据校验
if not token:
raise exceptions.AuthenticationFailed('用户认证失败')
return ('alex', None)
def authenticate_header(self, val):
pass
class DogView(APIView):
# 进行用户认证
authentication_classes = [Myauthentication, ]
def get(self, request, *args, **kwargs):
print(request)
# 此request和原生的request不同
# 是DRF的dispatch加工后的request
print(request.user)
# 即Myauthentication中的authenticate返回的元组
self.dispatch
ret = {
'code': 1000,
'msg': 'xxx'
}
return HttpResponse(json.dumps(ret), status=201)
def post(self, request, *args, **kwargs):
return HttpResponse('创建Dog')
def put(self, request, *args, **kwargs):
return HttpResponse('更新Dog')
def delete(self, request, *args, **kwargs):
return HttpResponse('删除Dog')
-
In the same way, according to the urls file, we can find
as_view()
the method first, -
If there is no custom one
DogView
, then go to its parent class, that isAPIView
,as_view()
-
It can be seen that it is the called
View
methodas_view()
, and through the above CBV analysis, it is finally theself
calleddispatch
method -
However, it should be noted that this example
self
refers to our customDogView
, so if you want to find itdispatch
, you need toDogView
find it layer by layer -
And
APIView
there aredispatch
methods in it, so go to the source code -
Original
request
processedintialize_request
, jump to source code -
One item is added
authenticators
, which is calledself.get_authenticators
. Similarly, this method also needs toDogView
be found from the beginning. I did not find it from the parent class. -
Instantiate
self.authentication_classes
the class list as an object and continue to jump -
You can
DogView
customize one inauthentication_classes
, you can override the default -
Go back to
APIView
the middledispatch
, findintial
the jump, (the parameter passed in is the processed request) -
continue to jump
-
Find the processed
request
(intialize_request
jump from the return value) -
Set the expansion on the left first
-
turn up
user
- continue to jump
-
Each of them
authenticator
has aauthenticate
method, so weMyauthentication
also need to write aauthenticate
-
In addition, a
authenticate_header
method is required
74)] -
Each of them
authenticator
has aauthenticate
method, so weMyauthentication
also need to write aauthenticate
-
authenticate_header
In addition, a method is required -
In this way, the basic user authentication is realized (the token in the url can be written casually at this time)