Routing System
When we start a project django, django want access to resources in the project through a browser
We need to configure routes in the urls in django projects
urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^test/', test1), ]
This function can be accessed in the browser jango
However, in this urls beginning of the file comments, you have told us later url can follow the following three
Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
After we learned that only when a route to a final match function, by the end of this route considered
1. Route matching with the function name
The first is direct with the above functions, represents a route terminates
2. Routing match with class method call as_view
The second is with the class, call as_view () method, we can look at as_view ways to help us achieve what
Function parentheses highest priority and takes precedence
First, take a class of binding method as_view
Then generates a closure in as_view View function, which function in the longevity of the object class instance, and the results back out dispatch method
Then as_view In fact, this view of the return value of the function object, so the essence of this approach is with the function
If you want to understand the implementation process cbv, you can then go look at the dispatch method, today we focus on explaining the following route distribution include
3. route distribution
Writing: distribution and routing to go through include
urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^test/', include('app01.urls')), ]
1. First, let's look at the internal functions include help us did what
Through the above analysis we know, include help us to do things that actually importing modules written by a string and returns a tuple of the form
It says include ( 'app01.urls'), and this formulation (urls, None, None) after the introduction is essentially no difference urls
2. Let us look url () did what
In this way we can generally understood to mean that he put our (module objects, None, None) converted into (list, None, None)
3. Since the analysis here, and then I direct route matches this (list, None, None) should also not be a problem
Indeed is the case, this is the essence of our route distribution
urlpatterns = [ url(r'^test/', ([ # 这里是分一级路由 url(r'^test_1', ([ # 这是分的二级路由,我们可以通过这种方式无限往下分 url(r'^test_1_1', test_1_1), url(r'^test_1_2', test_1_2) ],None, None)), url(r'^test_2', test_2), ],None, None)), ]
注意: 只有当路由匹配后面跟的是函数对象时,这条路由才算结束,否则如果是([],None,None)这种,接着往下分,往下找。
路由系统
当我们启动一个django项目后,想要通过浏览器访问到django项目中的资源
就需要在django中的urls项目中进行路由配置
urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^test/', test1), ]
这样就可以在浏览器访问到jango中的函数
然而在这个urls文件开头注释中,就已经告诉了我们url后面可以跟以下三种
Function views 1. Add an import: from my_app import views 2. Add a URL to urlpatterns: url(r'^$', views.home, name='home') Class-based views 1. Add an import: from other_app.views import Home 2. Add a URL to urlpatterns: url(r'^$', Home.as_view(), name='home') Including another URLconf 1. Import the include() function: from django.conf.urls import url, include 2. Add a URL to urlpatterns: url(r'^blog/', include('blog.urls'))
我们又了解到,只有当一条路由最后匹配到一个函数之后,这条路由才算结束
1. 路由匹配跟函数名
上面的第一种就是直接跟函数,表示一条路由的终止
2. 路由匹配跟类调用as_view方法
第二种跟的是类,调用了as_view()的方法,我们可以看一下as_view方法帮我们实现了什么
函数加括号优先级最高,会优先执行
首先,走了as_view的类绑定方法
然后在as_view中生成了一个闭包函数view,在该函数中把类实例化长生对象,并把dispatch方法的结果返出来
然后as_view的返回值其实这个view函数对象,所以这种方法的本质还是跟的函数
如果你想了解cbv的执行过程,可以接着去dispatch方法中看一看,今天我们着重讲解下面的路由分发include
3. 路由分发
写法: 通过include把以及路由分发下去
urlpatterns = [ url(r'^admin/', admin.site.urls), url(r'^test/', include('app01.urls')), ]
1. 首先我们来了解一下include函数内部帮我们干了什么
通过以上的分析我们知道,include帮我们干的事情其实是导入字符串写的模块,并以元组的形式返回
也就说 include('app01.urls') 和 导入urls之后的这种写法 (urls, None, None) 本质上是没有区别的
2. 我们再来看一下url()干了什么事
这样我们大致可以理解为,他把我们的(模块对象,None,None) 转换成了(列表,None,None)
3. 既然分析到了这里, 那么我直接在路由匹配通过这种 (列表,None,None) 应该也是不成问题的
确实就是如此,这也是我们路由分发的本质
= the urlpatterns [ URL (R & lt ' ^ Test / ' , ([ # Here is a partial route URL (R & lt ' ^ TEST_1 ' , ([ # which is two route points, we can in this way divided down unlimited URL (R & lt ' ^ test_1_1 ' , test_1_1), URL (R & lt ' ^ test_1_2 ' , test_1_2) ], None, None)), URL (R & lt ' ^ test_2 ' , test_2), ], None, None)), ]
Note: Only when the routing function match is followed by an object, considered the end of the route, or if it is ([], None, None) This, then points down, looking down.