7.9. Django routing

发布时间 :2025-10-25 12:24:13 UTC      

Simply speaking, routing is to judge the corresponding handler according to the URL link requested by the user, and return the processing result, that is, the mapping relationship between URL and Django view is established.

Django routes are configured in urls.py, and each configuration in urls.py corresponds to the corresponding processing method.

The configuration of urls.py is a little different in different versions of Django:

7.9.1. Django1.1.x version

url() 方法 Both normal path and regular path can be used, and you need to manually add regular first limit symbols.

Example

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

7.9.2. Django versions later than 2.2.x

  • Path: for ordinary paths, you do not need to manually add regular first limit symbols, the underlying layer has been added.

  • re_path:用于正则路径,需要自己手动添加正则首位限制符号。

Example

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), # 正则路径
]

总结: Url in the Django1.1.x version is the same as re_path in the Django 2.2.x version.

7.9.3. Grouping in regular paths

7.9.4. Unnamed groups in regular paths

Anonymous groups transmit parameters according to location, corresponding one by one.

Except for request, the number of other parameters in views should be the same as the number of groups in urls.

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path("^index/([0-9]{4})/$", views.index),
]

views.py

from django.shortcuts import HttpResponse
def index(request,year):
    print(year) # 一个形参代表路径中一个分组的内容,按顺序匹配
    return HttpResponse('菜鸟教程')

image0

7.9.5. Named grouping in regular paths

Syntax:

(?P<组名>正则表达式)

The named group transmits parameters by keyword, regardless of the order of position.

Except for request, the number of other parameters in views should be the same as the number of groups in urls, and the names of parameters in views should correspond to the group names in urls.

urls.py

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path("^index/(?P[0-9]{4})/(?P[0-9]{2})/$", views.index),
]

views.py

from django.shortcuts import HttpResponse
def index(request, year, month):
    print(year,month) #
一个形参代表路径中一个分组的内容,按关键字对应匹配
    return HttpResponse('菜鸟教程')

image1

7.9.6. Named grouping in regular paths

7.9.7. Route Distribution (include)

存在问题 Sharing one urls among multiple app directories in a Django project is easy to cause confusion and inconvenient for later maintenance.

解决 Using routing distribution (include), each app directory has its own urls.

步骤:

  • 1、在每个 app 目录里都创建一个 urls.py 文件。

  • 2、在项目名称目录下的 urls 文件里,统一将路径分发给各个 app 目录。

Example

from django.contrib import admin
from django.urls import path,include # 从 django.urls 引入 include
urlpatterns = [
    path('admin/', admin.site.urls),
    path("app01/", include("app01.urls")),
    path("app02/", include("app02.urls")),
]

image2

In the respective app directory, write your own urls.py file and 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),
]

image3

image4

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

7.9.8. Reverse parsing

With the increase of function, the url of the routing layer changes, so 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 parsing. When the url of the routing layer changes, we can dynamically reverse the changed url in the view layer and template layer to avoid modification.

Reverse parsing is commonly used in hyperlinks in templates and redirects in views.

7.9.9. Ordinary path

Alias the route in urls.py name=”路由别名” .

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

image5

In views.py, introduce reverse from django.urls, using the reverse(“路由别名”) Reverse resolution:

return redirect(reverse("login"))

image6

In the HTML file in the template templates, use the {% url “路由别名” %} Reverse parsing.

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

image7

7.9.10. Regular path (anonymous grouping)

Alias the route in urls.py name=”路由别名” .

re_path(r"^login/([0-9]{2})/$", views.login, name="login")

image8

In views.py, introduce reverse from django.urls, using the reverse(“路由别名”,args=(符合正则匹配的参数,)) Reverse parsing.

return redirect(reverse("login",args=(10,)))

image9

In the HTML file in the template templates {% url “路由别名” 符合正则匹配的参数 %} Reverse parsing.

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

image10

7.9.11. Regular path (named grouping)

Alias the route in urls.py name=”路由别名” .

re_path(r"^login/(?P<year>[0-9]{4})/$", views.login, name="login")

image11

In views.py, introduce reverse from django.urls, using the reverse(“路由别名”,kwargs={“分组名”:符合正则匹配的参数}) Reverse parsing.

return redirect(reverse("login",kwargs={"year":3333}))

image12

In the HTML file in the template templates, use the {% url “路由别名” 分组名=符合正则匹配的参数 %} Reverse parsing.

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

image13

7.9.12. Namespace

A namespace (English: Namespace) is a visible range that represents an identifier.

An identifier can be defined in multiple namespaces, and its meaning in different namespaces is irrelevant.

Any identifiers can be defined in a new namespace, and they do not conflict with any duplicate identifiers, because duplicate definitions are in other namespaces.

存在问题: The routing alias name has no scope. When Django parses the URL in reverse, it will search in the global order of the project. When it finds the first routing alias, name, which specifies URL, it will return immediately. When the same routing alias name is defined in urls under different app directories, it may result in URL reverse parsing error.

解决: Use namespaces.

7.9.13. Ordinary path

Define the namespace (there is a tuple in include) in the following format:

include(("app名称:urls","app名称"))

Example:

path("app01/", include(("app01.urls","app01")))
path("app02/", include(("app02.urls","app02")))

image14

Have the same routing alias in app01/urls.py.

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

image15

Using namespaces in views.py, the syntax format is as follows:

reverse("app名称:路由别名")

Example:

return redirect(reverse("app01:login")

image16

Use namespaces in the HTML file of the templates template in the following syntax format:

{% url "app名称:路由别名" %}

Example:

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

image17

Principles, Technologies, and Methods of Geographic Information Systems  102

In recent years, Geographic Information Systems (GIS) have undergone rapid development in both theoretical and practical dimensions. GIS has been widely applied for modeling and decision-making support across various fields such as urban management, regional planning, and environmental remediation, establishing geographic information as a vital component of the information era. The introduction of the “Digital Earth” concept has further accelerated the advancement of GIS, which serves as its technical foundation. Concurrently, scholars have been dedicated to theoretical research in areas like spatial cognition, spatial data uncertainty, and the formalization of spatial relationships. This reflects the dual nature of GIS as both an applied technology and an academic discipline, with the two aspects forming a mutually reinforcing cycle of progress.