Django Routers
Routing is simply determining the corresponding handler based on the URL link requested by the user and returning the result, which is establishing a mapping relationship between the URL and Django's views.
Django routing is configured in urls.py, and each configuration in urls.py corresponds to a corresponding handler method.
The urls.py configuration is slightly different in different Django versions:
### Django1.1.x Version
**url() method**: Can be used for both regular paths and regex paths, requires manually adding regex start/end limit symbols.
## Example
from django.conf.urls import url # Need to import url
urlpatterns =[
url(r'^admin/$', admin.site.urls),
url(r'^index/$', views.index),# Regular path
url(r'^articles/({4})/$', views.articles),# Regex path
]
### Django 2.2.x and later versions
* path: Used for regular paths, no need to manually add regex start/end limit symbols, already added at theUnderlying.
* re_path: Used for regex paths, requires manually adding regex start/end limit symbols.
## Example
from django.urls import re_path # Need to import re_path
urlpatterns =[
path('admin/', admin.site.urls),
path('index/', views.index),# Regular path
re_path(r'^articles/({4})/$', views.articles),# Regex path
]
**Summary:** The url in Django1.1.x version and re_path in Django 2.2.x version have the same usage.
* * *
## Groups in Regex Paths
### Unnamed Groups in Regex Paths
Unnamed groups pass parameters by position, one-to-one correspondence.
In views, besides request, the number of other parameters must match the number of groups in urls.
## urls.py
urlpatterns =[
path('admin/', admin.site.urls),
re_path("^index/({4})/$", views.index),
]
## views.py
from django.shortcuts import HttpResponse
def index(request, year):
print(year)# One parameter represents the content of one group in the path, matched by order
return HttpResponse('')
!(#)
### Named Groups in Regex Paths
Syntax:
(?Pregular expression)
Named groups pass parameters by keyword, unrelated to position order.
In views, besides request, the number of other parameters must match the number of groups in urls, and the parameter names in views must correspond to the group names in urls.
## urls.py
urlpatterns =[
path('admin/', admin.site.urls),
re_path("^index/(?P{4})/(?P{2})/$", views.index),
]
## views.py
from django.shortcuts import HttpResponse
def index(request, year, month):
print(year,month)# One parameter represents the content of one group in the path, matched by keyword
return HttpResponse('')
!(#)
### Named Groups in Regex Paths
### Route Dispatching (include)
**Problem:** Having multiple app directories in a Django project share one urls can easily cause confusion and is inconvenient for later maintenance.
**Solution:** Use route dispatching (include) so that each app directory has its own urls.
**Steps:**
* 1. Create a urls.py file in each app directory.
* 2. In the urls file under the project name directory, uniformly dispatch paths to each app directory.
## Example
from django.contrib import admin
from django.urls import path,include # Import include from django.urls
urlpatterns =[
path('admin/', admin.site.urls),
path("app01/", include("app01.urls")),
path("app02/", include("app02.urls")),
]
!(#)
Write your own urls.py file in each app directory to perform path jumping.
app01 directory:
from django.urls import path,re_path from app01 import views # Import views from your own app directory
urlpatterns = [
re_path(r'^login/(?P{2})/$', views.index, ),
]
app02 directory:
from django.urls import path,re_path from app02 import views # Import views from your own app
YouTip