Before we start, let's take a picture to understand the internal Django process, and we know that Django uses the MTV architecture, and the first part today is the controller, called the routing system in the Tornado framework, that maps the URLs to the appropriate processing logic. In Django, which is view processing, called views, and presumably, I'll go over and see how this dispatcher is implemented and how to use it.
A mapping table of URL patterns and view functions
Urlpatterns = [ url (R ' ^admin/', admin.site.urls), url (r ' ^index/', Views.index),]
Why is the URL pattern, because in the URL is a regular expression to do one by one mapping, through the regular expression, you can implement to map a number of similar URLs to the same view function for statistical processing
URL (regular expression, view function, parameter, alias)
Regular expressions flexibly match the URL you want
Urlpatterns = [ url (R ' ^articles/2003/$ ', views.special_case_2003), url (r ' ^articles/([0-9]{4})/$ ', views.year_archive), 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),]
Note here that the front-end submitted data encapsulated in the request object, in the definition of the view function requires a parameter to accept the object, the parameter name is arbitrarily named, the regular expression involves the use of groups to capture, in the view function also to use parameters to accept, there are several groups, the number of parameters to receive
For example, there are two groups in a regular expression.
def index (req,num,num2): print (num) print (num2) if Req.method = = "POST": name = req. Post.get (' Name ', None) pwd = req. Post.get (' Pwd ', None) print (name,pwd) return HttpResponse (' success ') return render (req, ' index.html ') )
In addition, the regular expression is supported by the specified group name, if the regular expression used in this way to match, in the View function, the name of the receiving parameter can not be random, must be specified if the group name
Urlpatterns = [ url (R ' ^admin/', admin.site.urls), url (r ' ^index/(? p<n1>\d) (? p<n2>\d) ', views.index),]def Index (REQ,N1,N2): print (n1) print (n2) if Req.method = = "POST": Name = req. Post.get (' Name ', None) pwd = req. Post.get (' Pwd ', None) print (name,pwd) return HttpResponse (' success ') return render (req, ' index.html ')
The second parameter, not only can be passed into views of the view function, but also can be passed into the include implementation of the application routing distribution, so as to achieve the role of loose coupling, a big point of the company will have a number of business, will generally be divided into multiple apps by business sector
, the route map can be done without interfering with the URL.
To implement routing distribution three steps away:
The first step is to import the include in the previous URL file and join the distribution mapping relationship
From django.conf.urls import urlfrom django.contrib import adminfrom app01 import viewsfrom django.conf.urls import includ Eurlpatterns = [ url (R ' ^admin/', admin.site.urls), url (r ' ^index/(? p<n1>\d) (? p<n2>\d) ', Views.index), #这里实现路由分发 url (r ' ^car/', include (' App01.urls ')),]
The second step, create a urls.py file under the app, add the URL and the View function mapping relationship
From Django.conf.urls import Urlfrom. Import viewsurlpatterns = [ url (' buy/(? P<year>\d{4}) (? p<month>\d) ', Views.home)]
The third step, define the View function
def home (req,year,month): print (year) print (month) return httpresponse (' OK ')
Direct access to http://127.0.0.1:8000/car/buy/12345.
The third parameter: You can pass the value of the dictionary to the view function, the view function must have parameters to receive
From Django.conf.urls import Urlfrom. Import viewsurlpatterns = [ url (' buy/(? P<year>\d{4}) (? p<month>\d) ', views.home,{' foo ': ' Bar '}]
def home (Req,year,month,foo): Print (foo) print (year) print (month) return httpresponse (' OK ')
If the URL mapping relationship in the app adds this parameter, only works on the current URL, if you add this parameter to the routing distribution, it is the URL of the entire app, which you use to implement different requirements for the URL, such as different URLs may be implemented to connect different databases, This can be used to pass an operation handle, or a different URL to implement what kind of cache
From django.conf.urls import urlfrom django.contrib import adminfrom app01 import viewsfrom django.conf.urls import includ Eurlpatterns = [ url (R ' ^admin/', admin.site.urls), url (r ' ^index/(? p<n1>\d) (? p<n2>\d) ', Views.index), #全局f url (r ' ^car/', include (' App01.urls '), {' F ': ' F '}),]
From Django.conf.urls import Urlfrom. Import viewsurlpatterns = [ #局部foo url (' buy/(? P<year>\d{4}) (? p<month>\d) ', views.home,{' foo ': ' Bar '}, url (' Money ', Views.money),]
def home (req,year,month,foo,f): Print (f) print (foo) print (year) print (month) return HttpResponse (' OK ') def money (req,f): print (f) return HttpResponse (' OK ')
The four parameters: aliases, when the URL changes, the front end through the alias of post data, can still access the same content, into the same logic
<! DOCTYPE html>
Urlpatterns = [ url (R ' ^admin/', admin.site.urls), url (r ' ^index/(? p<n1>\d) (? p<n2>\d) ', Views.index ', url (r ' ^bieming ', views.bieming,name= ' bieming '), #全局f url (r ' ^car/', Include (' App01.urls '), {' F ': ' F '}),]
Django Distribution controller urls--vernacular chat Django series