12.36 Django routing system
The basic format:
from django.conf.urls Import URL the urlpatterns = [ URL (regex, views view function, parameter, alias) ] regular expression: a regular expression string views view function: a callable object, typically a function specified view or view a string of path function parameters: the optional parameters to be passed to the default view function (a dictionary) alias: an optional parameter name
12.361 routing Regular Expressions
from django.conf.urls Import URL from django.contrib Import ADMIN from app01 Import views the urlpatterns = [ URL (R & lt ' ^ ADMIN / ' , admin.site.urls), URL (R & lt ' ^ book_list / ([0-9 ] {}. 4) / (\ D {2}) ' , views.book_list), # by brackets capturing position parameters: (4 digits), (2 digits) # be publisher_list (Request, X, Y) URL (R & lt ' ^ be publisher_list / (? P <n-> [0-9] {}. 4) / (? P <m> \ D {2}) ' , views.publisher_list), #publisher_list (request, n, m) # key parameters by capturing parentheses # passing additional parameters to view the function (learn) URL (R & lt ' ^ the demo1 / ([0-9] {}. 4) / (\ D {2} ) / $ ' , views.demo1, { " name " : " Egon " }), # optional parameters to be passed to a dictionary view function, and if the key dictionary matching packet naming conflict, the additional dictionary form parameters prevail # View function sets the default value of the parameter URL (R & lt ' ^ Blog / $ ' , views.page), # default value. 1 = NUM URL (R & lt ' ^ Blog / (? P <NUM> [0-9 ] +) / $ ' , views.page), # use their values to match ] # the views.py may specify default values for num def page(request, num="1"): pass
1, urlpatterns elements in the writing order from the top one by matching a regular expression, once the match is successful discontinued. 2, to capture a value from the URL, only to place a pair of parentheses (around which the packet match ). 3, do not need to add a leading backslash, because each URL has. For example, it should be ^ articles, not ^ / articles. 4, the front of each regex 'r' is optional but suggested adding
note:
# Whether to open the URL address is not accessible behind the path '/' to jump with '/' is CI the APPEND_SLASH = True # capture parameters always string # matched packet, the packet matching name can not be used mixed with
12.362 include other URLconfs
from django.conf.urls Import the include, URL the urlpatterns = [ URL (R & lt ' ^ ADMIN / ' , admin.site.urls), URL (R & lt ' ^ app01 / ' , the include ( ' app01.urls ' )), # can URLconfs contain other documents ]
12.363 URL name and URL reverse lookup
url (R & lt ' ^ Home ' , views.home, name = ' Home ' ), # from a url alias matching mode Home url (R & lt ' ^ index / (\ D *) ' , views.index, name = ' index ' ), # a named index matching mode url
Find url in the template language, according to an alias:
# No parameters: {% URL ' Home ' % } # positional parameters: {% URL " Home " " 2019 " " . 9 " % } # key parameters: {% URL " Home " " year " = 2019 " month The " = 9 %}
Find url in views.py according Alias:
from django.urls Import Reverse from django.shortcuts Import the redirect DEF redirect_to_year (Request): year = 2006 # no parameters URL: reverse ( 'alias') # positional parameters: reverse ( "alias", args = ( "2018" , )) # key parameters: reverse ( "alias", kwargs = { "k1" : parameter. 1, ...}) return the redirect (Reverse ( ' News-year-Archive ' , args = (year,))) return the redirect (Reverse ( ' News-year-Archive ' , kargs = { ' year ' : 2006}))
12.364 namespace mode
project in urls.py:
from django.conf.urls import url, include urlpatterns = [ url(r'^app01/', include('app01.urls', namespace='app01')), url(r'^app02/', include('app02.urls', namespace='app02')), ]
app01 in urls.py:
from django.conf.urls import url from app01 import views app_name = 'app01' urlpatterns = [ url(r'^(?P<pk>\d+)/$', views.detail, name='detail') ]
app02 in urls.py:
from django.conf.urls import url from app02 import views app_name = 'app02' urlpatterns = [ url(r'^(?P<pk>\d+)/$', views.detail, name='detail') ]
Now, two app name in the url repeated, time reversal of the URL you can get through the current name of the namespace URL
Find url in the template language, according to an alias:
{% URL " namespace: Alias " parameter 1, parameter 2 ...% } # no parameters: {% URL ' app01: Detail ' % } # positional parameters: {% URL " app01: Detail " " 2019 " " . 9 " % } # key parameters: {% URL ' app01: Detail ' PK = 12 is% } { % URL " app01: Detail " ," year " = 2019 " month The " =}. 9%
Find url in views.py according Alias:
from django.urls Import Reverse from django.shortcuts Import redirect DEF redirect_to_year (Request): year = 2006 # no reference URL: reverse ( 'namespace: Alias') # positional parameters: reverse ( "namespace: Alias", args = (parameter 1, parameter 2, ...)) # key parameters: reverse ( "namespace: alias", kwargs = { "k1" : parameter 1, ...}) return the redirect (Reverse ( ' app02: Detail ' , args = (year,))) return the redirect (Reverse ( ' app02: Detail ' , kwargs = { ' PK ' :}. 11))
Even though the app the same name in the URL can also be reversed to get the correct URL