The road to WeChat applet development (8) Django routing

The road to WeChat applet development (8) Django routing

Simply put, routing is to determine the corresponding processing program based on the URL link requested by the user, and return the processing result, that is, the URL and the Django view establish a mapping relationship.
Django routing is configured in urls.py, and each configuration in urls.py corresponds to the corresponding processing method.
The urls.py configuration of different versions of Django is a bit different (version 3.2.10 used by bloggers):
path: used for ordinary paths, you don't need to manually add the regular first restriction symbol yourself, the bottom layer has been added.
re_path: used for regular path, you need to manually add the first restriction symbol of regular.

Examples:

from django.urls import re_path # 用re_path 需要引入
urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/', views.index), # 普通路径
    re_path(r'^articles/([0-9]{4})/$', views.articles), # 正则路径
]

Grouping in regular path

Unnamed grouping in the regular path The unnamed grouping
is passed parameters by position, one to one correspondence.
Except for request, the number of other parameters in views must be the same as the number of groups in urls.

from django.conf.urls import url
from django.urls import re_path
from django.urls import path
from django.contrib import admin
from . import views,testdb,search,search2
 
urlpatterns = [
    path('admin/', admin.site.urls),
    re_path("^index/([0-9]{4})/$", views.index),
]

Insert picture description here
It is best to teach yourself regular expressions:
Insert picture description here

from django.http import HttpResponse
from django.shortcuts import render
def runoob(request):
    name ="中国矿业大学计算机学院许磊"
    return render(request,"runoob.html",{
    
    "name":name})
def index(request,year):
    print(year) # 一个形参代表路径中一个分组的内容,按顺序匹配
    return HttpResponse('中国矿业大学计算机学院许磊')

Insert picture description here
Named grouping in regular path
Syntax: (?P<group name> regular expression)
Named grouping is passed parameters by keywords, regardless of position order.
Except for request, the number of other parameters in views must be the same as the number of groups in urls, and the names of the parameters in views must correspond to the group names in urls.Insert picture description here
Insert picture description here

Named group in regular path


There is a problem with routing distribution (include) : multiple app directories in the Django project share one urls, which is easy to cause confusion and inconvenient maintenance.

Solution : Use route distribution (include), so that each app directory has its own urls.

step:

1、在每个 app 目录里都创建一个 urls.py 文件。
2、在项目名称目录下的 urls 文件里,统一将路径分发给各个 app 目录。

Insert picture description here
In the respective app directory, write your own urls.py file to jump to the path.
app01 directory:

from django.urls import path,re_path 
from app01 import views # 从自己的 app 目录引入 views 
urlpatterns = [ 
    re_path(r'^login/(?P<m>[0-9]{2})/$', views.index, ),
] 

app02 directory:

from django.urls import path,re_path
from app02 import views # 从自己的 app 目录引入views 
urlpatterns = [ 
    re_path("^xxx/(?P[0-9]{4})/$", views.xxx), 
]

Write the respective view functions in the views.py file in the respective app directory.

Reverse analysis

With the increase of functions, the URL of the routing layer changes, it is necessary to change the URL of the corresponding view layer and template layer, which is very troublesome and inconvenient to maintain.
At this time, we can use reverse analysis. When the routing layer URL changes, the modified URL can be dynamically resolved in the view layer and template layer, eliminating the need for modification.
Reverse analysis is generally used in hyperlinks in templates and redirects in views.
Common path
Alias ​​the route in urls.py, name="route alias".

path("login1/", views.login, name="login")

In views.py, introduce reverse from django.urls, and use reverse ("route alias") to reverse the resolution:

return redirect(reverse("login"))

In the HTML file in the template templates, use {% url "route alias" %} for reverse analysis.

<form action="{% url 'login' %}" method="post"> 

Namespaces

Namespace (English: Namespace) is the visible range of identifiers.
An identifier can be defined in multiple namespaces, and its meaning in different namespaces is irrelevant.
Any identifier can be defined in a new namespace, and they will not conflict with any repeated identifiers, because the repeated definitions are in other namespaces.
There is a problem: the routing alias name has no scope. Django will search in the global order of the project when it parses the URL in the reverse direction. When it finds the URL specified by the first routing alias name, it will return immediately. When the same routing alias name is defined in urls in different app directories, it may cause URL reverse parsing errors.
Solution: Use namespace.
The
format of the common path definition namespace (include is a tuple) is as follows:

include(("app名称:urls""app名称"))
path("app01/", include(("app01.urls","app01"))) 
path("app02/", include(("app02.urls","app02")))

Guess you like

Origin blog.csdn.net/xulei1132562/article/details/113615811