django源码阅读笔记
文章目录
Open edX的后端采用Django来写,Django是一个大而全的web框架,许多地方和Rails相似
Open edX对Django框架做了大量的改造,对其特性的应用也是十分全面。由于时常需要去hack Open edX,以至于不得不深入到Django源码本身,读别人的源码,和大多事情一样,都符合万事开头难的规律,深入之后,其乐无穷.
初极狭,才通人,复行数十步,豁然开朗
Open edX的最新稳定版所依赖的Django版本为1.8.7,所以我主要阅读的也是这个版本的源码:Django 1.8.7
下面下阅读过程一些值得记录的地方记下来
#django-admin
安装django后,我们会获得一个命令行工具django-admin
,用于创建django项目和djangoapp
这主要是通过entry_points实现
|
|
通过entry_points,我们可以将python函数注册到系统,这对于用python写系统应用十分有用
#request 首先来看看HttpRequest
|
|
###QueryDict request的两个GET和POST属性是QueryDict. QueryDict集成自MultiValueDict
MultiValueDict来自django/django/utils/datastructures.py,是django为自身打造的一种抽象数据结构,这个抽象数据结构主要是为了解决这个问题
This class exists to solve the irritating problem raised by cgi.parse_qs,which returns a list for every key, even though most Web forms submitsingle name-value pairs.
###MultiPartParser MultiPartParser类的主要作用是:
Multi-part parsing for file uploads.
更多的可用属性和方法参考:Request and response objects
#response HttpResponse
An HTTP response class with a string as content. This content that can be read, appended to or replaced.
###JsonResponse django/django/http/response.py JsonResponse
|
|
#middleware
Middleware is a framework of hooks into Django’s request/response processing. It’s a light, low-level “plugin” system for globally altering Django’s input or output.
我们关注一下几个middleware
|
|
#####process_request
|
|
#####process_response
process_response() is called on all responses before they’re returned to the browser.
|
|
###CommonMiddleware 使用条件分支来过滤非法客户端
|
|
ETag header的处理也在CommonMiddleware
###CsrfViewMiddleware #####process_view
|
|
callback come from def process_view(self, request, callback, callback_args, callback_kwargs):
,callback是装饰器?
###AuthenticationMiddleware #####process_request
|
|
注意断言(assert)的使用,这里设置了request.user,并且
The Django authentication middleware requires session middleware
###get_user get_user
#backends
###ModelBackend
By default, AUTHENTICATION_BACKENDS is set to: ['django.contrib.auth.backends.ModelBackend']
###CASBackend look at CASBackend
#storage system
###qiniu
#参考
文章作者 种瓜
上次更新 2016-03-07