python's Django framework (routing system, include, naming URL and URL reverse parsing, namespace schema)

12.36 Django Routing System

Basic format:

from django.conf.urls import url
urlpatterns = [
     URL (regular expressions, views view functions, parameters, aliases)
]
​
Regular expression: A regular expression string
views view function: A callable object, usually a view function or a string specifying the path of a view function
Parameters: Optional default parameters to be passed to the view function (dictionary form)
Alias: An optional name parameter
12.361 Routing Regular Expressions
from django.conf.urls import url
from django.contrib import admin
from app01 import views
​
urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^book_list/([0-9]{4})/(\d{2})', views.book_list),#Capturing position parameters by parentheses:(4 Number of digits),(2 Number of digits)
    # publisher_list(request, x, y)
    
    url(r'^publisher_list/(?P<n>[0-9]{4})/(?P<m>\d{2})', views.publisher_list),
    # publisher_list(request, n, m)     #Capture keyword parameters by parentheses
    
    #Pass additional parameters to the view function (understand)
    url(r'^demo1/([0-9]{4})/(\d{2})/$', views.demo1, {"name": "egon"}),
    #Optional dictionary-form parameters to be passed to view functions,If the dictionary's key In case of conflict with group naming, additional parameters in dictionary form shall prevail.
#view Function Setting Default Value Parameters  
    url(r'^blog/$', views.page),                #Use default values num=1 
    url(r'^blog/(?P<num>[0-9]+)/$', views.page), #Use your own matched values 
]
# views.py In Chinese, we can num Specify default values
def page(request, num="1"):
    pass

1. The elements in urlpatterns match the regular expressions one by one from top to bottom in writing order. Once the matching is successful, they do not continue. 2. To capture a value from a URL, just place a pair of parentheses around it (grouping matching). 3. There is no need to add a leading backslash, because every URL has one. For example, it should be ^ articles rather than ^ / articles. 4. The'r'before each regular expression is optional, but it is recommended to add

Be careful:

#Whether to open or not URL Not after the access address'/'Jump to the band'/'Path Configuration Item
APPEND_SLASH=True
#The captured parameters are always strings
#Group Matching, Group Naming Matching, Not Mixed
12.362 include other URLconfs
from django.conf.urls import include, url
urlpatterns = [
   url(r'^admin/', admin.site.urls),
   url(r'^app01/', include('app01.urls')),  # Can include other URLconfs file
]
12.363 Named URLs and Reverse Resolution of URLs
url(r'^home', views.home, name='home'),          #  to url Matching pattern alias home
url(r'^index/(\d*)', views.index, name='index'),  #  to url The matching pattern is named index

Find url in template language by alias:

#No reference:
{% url 'home' %}
#Position parameters:
{% url "home" "2019" "9" %}
#Keyword parameters:
{% url "home" "year"=2019 "month"=9 %}

Find url in views.py by alias:

from django.urls import reverse
from django.shortcuts import redirect
​
def redirect_to_year(request):
    year = 2006
    #No ginseng URL: reverse('alias')
    #Position parameters: reverse("alias", args=("2018", ))
    #Keyword parameters: reverse("alias", kwargs={"k1":Parametric 1, ...})
    return redirect(reverse('news-year-archive', args=(year,)))
    return redirect(reverse('news-year-archive', kargs={'year':2006}))
12.364 namespace schema

urls.py in project:

from django.conf.urls import url, include
 
urlpatterns = [
    url(r'^app01/', include('app01.urls', namespace='app01')),
    url(r'^app02/', include('app02.urls', namespace='app02')),
]

urls.py in app01:

from django.conf.urls import url
from app01 import views
 
app_name = 'app01'
urlpatterns = [
    url(r'^(?P<pk>\d+)/$', views.detail, name='detail')
]

urls.py in app02:

from django.conf.urls import url
from app02 import views
 
app_name = 'app02'
urlpatterns = [
    url(r'^(?P<pk>\d+)/$', views.detail, name='detail')
]

Now that the URL names in the two app s are repeated, you can get the current URL by inverting the name of the namespace

Find url in template language by alias:

{% url "Namespace:alias" Parametric 1, Parametric 2... %}
#No reference:
{% url 'app01:detail' %}
#Position parameters:
{% url "app01:detail" "2019" "9" %}
#Keyword parameters:
{% url 'app01:detail' pk=12  %}
{% url "app01:detail" "year"=2019 "month"=9 %}

Find url in views.py by alias:

from django.urls import reverse
from django.shortcuts import redirect
​
def redirect_to_year(request):
    year = 2006
    #No ginseng URL: reverse('Namespace:alias')
    #Position parameters: reverse("Namespace:alias", args=(Parameter 1, parameter 2, ...))
    #Keyword parameters: reverse("Namespace:alias", kwargs={"k1":Parametric 1, ...})
    return redirect(reverse('app02:detail', args=(year,)))
    return redirect(reverse('app02:detail', kwargs={'pk':11}))

This way, even if the name of the URL in the app is the same, the correct URL can be inverted.

Tags: Python Django

Posted on Tue, 03 Sep 2019 07:47:14 -0700 by localhost1