一 CBV源码流程

urls.py

from django.conf.urls import url
from django.contrib import admin
from app01 import views urlpatterns = [
url(r'^order/', views.OrderView.as_view()),
]

  

view.py

from django.shortcuts import render
from django.http import JsonResponse
from django.views import View
class OrderView(View): def get(self,request,*args,**kwargs):
ret = {'code': 1000, 'msg': None, 'error': None}
return JsonResponse(ret)

  

1)从上面的urls.py文件种可以看到,一个url对应了一个  这个views.OrderView.as_view()函数,并执行这个函数,也就是我们调用order的url会views.OrderView.as_view()()

2)从views.py文件种看到OrderView这个类种,并没有as_view()方法,所以需要到View类种找

    # 这是一个类方法
@classonlymethod
def as_view(cls, **initkwargs):
"""
Main entry point for a request-response process.
"""
for key in initkwargs:
if key in cls.http_method_names:
raise TypeError("You tried to pass in the %s method name as a "
"keyword argument to %s(). Don't do that."
% (key, cls.__name__))
if not hasattr(cls, key):
raise TypeError("%s() received an invalid keyword %r. as_view "
"only accepts arguments that are already "
"attributes of the class." % (cls.__name__, key)) def view(request, *args, **kwargs):
# self = OrderView()
self = cls(**initkwargs)
if hasattr(self, 'get') and not hasattr(self, 'head'):
self.head = self.get
self.request = request
self.args = args
self.kwargs = kwargs
# handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
# return handler(request, *args, **kwargs)
# 这个as_view()返回了一个OrderView().dispatch(request, *args, **kwargs)方法
# 这里返回了dispatch(),所以这个dispath方法会被执行,但是需要从头开始找起
return self.dispatch(request, *args, **kwargs)
view.view_class = cls
view.view_initkwargs = initkwargs # take name and docstring from class
update_wrapper(view, cls, updated=()) # and possible attributes set by decorators
# like csrf_exempt from dispatch
update_wrapper(view, cls.dispatch, assigned=())
# 由于调用url会views.OrderView.as_view()()所以,这里会执行view函数
return view def dispatch(self, request, *args, **kwargs):
# Try to dispatch to the right method; if a method doesn't exist,
# defer to the error handler. Also defer to the error handler if the
# request method isn't on the approved list.
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
# 最终返回了执行后的handler()
return handler(request, *args, **kwargs)

  

最新文章

  1. easyui的textbox赋值小结
  2. Django 基础(一)
  3. jquery注意
  4. Delphi中CoInitialize之探究
  5. kuangbin_ShortPath R (HDU 4370)
  6. oracle中统计重复几次的数据有几条
  7. ubuntu安装后没有root密码
  8. WIN7下关闭驱动数字签名检查的方法
  9. Swift - 基本数据类型,及常/变量声明
  10. JWebFileTrans(JDownload): 一款可以从网络上下载文件的小程序(二)
  11. 【原创】源码角度分析Android的消息机制系列(五)——Looper的工作原理
  12. 常见web安全隐患及解决方案
  13. 网络协议 15 - P2P 协议:小种子大学问
  14. 两层fragment嵌套时出现空白,(收藏别人的)
  15. eclipse经常出现——未响应!!!
  16. noip第15课作业
  17. SPI、I2C、UART三种串行总线协议的区别和SPI接口介绍(转)
  18. [ntp]查看ntp服务器的连接情况
  19. Hadoop streaming使用自定义python版本和第三方库
  20. spring框架中工厂方法的创建和销毁

热门文章

  1. SpringBoot项目属性配置
  2. 使用 Visual Studio 将 ASP.NET Web 应用部署到 Azure
  3. Token Based Authentication -- Implementation Demonstration
  4. mysql 表的增删改查
  5. tcp/udp/socket 端口映射,转发小工具
  6. MapReduce 过程详解 (用WordCount作为例子)
  7. springcloud15---zuul-fallback
  8. HTTP从入门到入土(3)——TCP三次握手
  9. java 读CSV 和 Excel
  10. Java生成PDF之iTextPDF的使用