Django Framework, urls.py module, views.py module, route map and routing distribution, and logic processing--url controller
This section is a presenter of the URL controller
One, urls.py module
This module is the module that configures the route map, when the user accesses a URL address, through this route mapping module, maps to the corresponding logical processing function
A list of urlpatterns equals one of the elements in the table is a route map
Urlpatterns route Map Configuration method
Parameter description:
A regular expression string
A callable object, typically a view function or a string that specifies the path of a view function
Optional default parameters to pass to the view function (in dictionary form)
An optional name parameter
Urlpatterns = [ URL (regular expression, mapping function, parameter [optional], alias [optional]),]
Urlpatterns = [ url (R ' ^admin/', admin.site.urls,{' a ': ' 123 '}, ' admin '),]
Such as:
"" "Xiangmu URL configurationthe ' urlpatterns ' list routes URLs to views. For more information see:https://docs.djangoproject.com/en/1.10/topics/http/urls/examples: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 ')) "" "from Django.conf.urls import urlfrom django.contrib impo RT Adminfrom App1 Import viewsurlpatterns = [url (r ' ^admin/', admin.site.urls), #系统生成的映射 #注意里面的任意一条映射匹配成功, the following is not the matching URL (r ' ^articles/2003/$ ', views.special_case_2003), #表示articles/2003/This path maps the special_case_2003 function # URL of the Views module (R ' ^articles/([0-9]{4})/$ ', views.year_archive), #表示2003可以是0-9 of any 4 number # URL (r ' ^articles/([0-9]{4})/([0-9]{2}) /$ ', views.month_archive), #表示匹配二级目录 # URL (r ' ^articles/([0-9]{4})/([0-9]{2})/([0-9]+]/$ ', Views.article_deta IL), #表示匹配三级目录]
Second, views.py module, the function module of route map, the requirement of logic processing route map
Note: There are two key points when customizing the mapping function
HttpResponse (String) method returns a string to the user
1, the defined function must, define a formal parameter, this form parameter receives the URL request information object, can obtain the various request information through the various methods of this form parameter
2, return information to the user, must return at the end of the function, if you want to return a string to the user, it must be returned HttpResponse method, parameter is the string to return, you need to import this method first
From django.shortcuts import render,httpresponse# Create your views here.def special_case_2003 (Request): print ( Request.method) #获取用户请求的路径 return httpresponse (' Hello ')
Last Test.
Browser input: http://127.0.0.1:8000/articles/2003/
Get a user access path when processing logic
The second parameter of the logical processing custom function is to receive the user request path, so a custom formal parameter is required to receive
From django.shortcuts import render,httpresponse# Create your views here.def special_case_2003 (request,name): Print (Request.method) #获取用户请求的路径 Print (name) #打印路径 return HttpResponse (name) #将路径返回到页面
Note: To get the path, you need to group the route map with the regular grouping () number, the path to be obtained, that is, if there are multiple groupings in the route map, the logical function needs to receive multiple form parameters
"" "Xiangmu URL configurationthe ' urlpatterns ' list routes URLs to views. For more information see:https://docs.djangoproject.com/en/1.10/topics/http/urls/examples: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 ')) "" "from Django.conf.urls import urlfrom django.contrib impo RT Adminfrom App1 Import viewsurlpatterns = [url (r ' ^admin/', admin.site.urls), #系统生成的映射 url (r ' ^ (articles/2003)/$ ' , views.special_case_2003), #表示articles/2003/This path maps the special_case_2003 function # URL of the Views module (R ' ^article s/([0-9]{4})/$ ', views.year_archive), #表示2003可以是0-9Any 4 number # URLs (R ' ^articles/([0-9]{4})/([0-9]{2})/$ ', views.month_archive), #表示匹配二级目录 # URL (r ' ^articles/([0 -9]{4})/([0-9]{2})/([0-9]+)/$ ', Views.article_detail), #表示匹配三级目录]
What we're talking about is the custom form parameter receive access path, let's talk about setting the fixed-form parameter
A logical handler that sets a fixed-form parameter when receiving a user's access path,
Need to set in route map, the parameter name that the logical function receives? P<year>
Route Map
"" "Xiangmu URL configurationthe ' urlpatterns ' list routes URLs to views. For more information see:https://docs.djangoproject.com/en/1.10/topics/http/urls/examples: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 ')) "" "from Django.conf.urls import urlfrom django.contrib impo RT Adminfrom App1 Import viewsurlpatterns = [url (r ' ^admin/', admin.site.urls), #系统生成的映射 url (r ' ^ (? P<year>articles/2003)/$ ', views.special_case_2003), #表示articles/2003/This path maps the SPECIAL_CASE_2003 function of the views module # URL (r ' ^articles/([0-9]{4})/$ ', views.year_archive), #表示2003可以是0-9 of any 4 number # URL (r ' ^articles/([ 0-9]{4})/([0-9]{2})/$ ', views.month_archive), #表示匹配二级目录 # URL (r ' ^articles/([0-9]{4})/([0-9]{2})/([0-9]+]/$ '), Views.article_detail), #表示匹配三级目录]
Logical processing
From django.shortcuts import render,httpresponse# Create your views here.def special_case_2003 (request,year): Print (year) return render (Request, ' index.html ')
Logical processing returns an HTML file
The HTML file is placed in the Templates folder, which is automatically searched for in the folder when it is logically processed.
You need to import the Render method first
Render (User request object, HTML file path name) method to return HTML file contents to the user
From django.shortcuts import render,httpresponse# Create your views here.def special_case_2003 (request,name): return render (Request, ' index.html ')
The entire process
Route Map
"" "Xiangmu URL configurationthe ' urlpatterns ' list routes URLs to views. For more information see:https://docs.djangoproject.com/en/1.10/topics/http/urls/examples: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 ')) "" "from Django.conf.urls import urlfrom django.contrib impo RT Adminfrom App1 Import viewsurlpatterns = [url (r ' ^admin/', admin.site.urls), #系统生成的映射 url (r ' ^ (articles/2003)/$ ' , views.special_case_2003), #表示articles/2003/This path maps the special_case_2003 function # URL of the Views module (R ' ^article s/([0-9]{4})/$ ', views.year_archive), #表示2003可以是0-9Any 4 number # URLs (R ' ^articles/([0-9]{4})/([0-9]{2})/$ ', views.month_archive), #表示匹配二级目录 # URL (r ' ^articles/([0 -9]{4})/([0-9]{2})/([0-9]+)/$ ', Views.article_detail), #表示匹配三级目录]
Logical processing
From django.shortcuts import render,httpresponse# Create your views here.def special_case_2003 (request,name): return render (Request, ' index.html ')
Entire flowchart
Above, all through the Global urls.py Module route mapping, if the site is very large number of apps, then need to route distribution, each app is responsible for a business
Routing distribution
Over a global urls.py module, configure routing distribution, distribute the developed path to the urls.py module in the specified app application, route map
Global urls.py Module Routing distribution
The first step in the global urls.py module is to introduce
From django.conf.urls import include, url
Include (' App1.urls ') function, set the route map path name to distribute
Global urls.py Module Routing distribution
From Django.conf.urls import include, urlurlpatterns = [ url (R ' ^bug ', include (' App1.urls ')), # Distribute the access path to the path at the beginning of the bug to the urls.py module under App1 for route mapping]
app1.py Route Map
From django.conf.urls import urlfrom django.contrib import adminfrom app1 Import viewsurlpatterns = [ url (r ' articles/ ', views.special), #表示接收全局的路由分发, do route mapping, map to special function processing under views]
views.py Logic Processing
From django.shortcuts import render,httpresponse# Create your views here.def Special (Request): return Render ( Request, ' index.html ') #向用户显示一个html页面
Route Map third parameter, additional argument, dictionary mode, logical handler receive dictionary key in parameter mode
Route Map
From django.conf.urls import urlfrom django.contrib import adminfrom app1 Import viewsurlpatterns = [ url (r ' articles/ ', views.special,{' Anme ': 1234}) #路由映射第三个参数, additional arguments, dictionary mode, logical processing functions receive dictionary keys in a parameter manner]
Logical processing
From django.shortcuts import render,httpresponse# Create your views here.def Special (REQUEST,ANME): print (ANME) #接收路由映射的额外传参字典的键 return render (Request, ' index.html ') #向用户显示一个html页面
Note: If additional parameters are written in the global routing distribution, all route mapping functions under this route distribution can be obtained
Route map the fourth parameter, the path of the route map to take an alias, this alias refers to the route map path,
From django.conf.urls import urlfrom django.contrib import adminfrom app1 Import viewsurlpatterns = [ url (r ' articles/ ', views.special,{' anme ': 1234},name= ' luj ') #路由映射第三个参数, additional arguments, dictionary mode, logical processing functions receive dictionary keys in a parameter manner]
Final URL Controller flowchart
Two Django Framework, urls.py module, views.py module, route map and routing distribution, and logic processing--url controller