Easy Tutorial
❮ Django Auth Django Cookie Session ❯

Django Routing

Simply put, routing is the process of determining the appropriate handler based on the user's requested URL and returning the processing result, which means establishing a mapping between the URL and Django's views.

Django routing is configured in urls.py, where each configuration corresponds to a specific handling method.

The configuration in urls.py differs slightly between different versions of Django:

Django 1.1.x Version

url() Method: Both regular and regex paths can be used, and you need to manually add regex boundary symbols.

Example

from django.conf.urls import url # Import url

urlpatterns = [
    url(r'^admin/$', admin.site.urls),
    url(r'^index/$', views.index), # Regular path
    url(r'^articles/([0-9]{4})/$', views.articles), # Regex path
]

Django 2.2.x and Later Versions

Example

from django.urls import re_path # Import re_path
urlpatterns = [
    path('admin/', admin.site.urls),
    path('index/', views.index), # Regular path
    re_path(r'^articles/([0-9]{4})/$', views.articles), # Regex path
]

Summary: The url method in Django 1.1.x and the re_path method in Django 2.2.x have the same usage.


Grouping in Regex Paths

Nameless Grouping in Regex Paths

Nameless grouping passes parameters by position, one-to-one correspondence.

In views, the number of parameters other than request must match 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) # One parameter represents the content of one group in the path, matched by order
    return HttpResponse('tutorialpro.org')

Named Grouping in Regex Paths

Syntax:

(?P<group_name>regex)

Named grouping passes parameters by keyword, independent of position.

In views, the number of parameters other than request 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<year>[0-9]{4})/(?P<month>[0-9]{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('tutorialpro.org')

Routing Distribution (include)

Issue: Having multiple app directories share a single urls file in a Django project can lead to confusion and difficulty in maintenance.

Solution: Use routing distribution (include) to allow each app directory to have its own urls.

Steps:

Example

from django.contrib import admin
from django.urls import path, include # Import include from django.urls
urlpatterns = [
    path('admin/', admin.site.urls),
    path('app1/', include('app1.urls')),
    path('app2/', include('app2.urls')),
]
path("app01/", include("app01.urls")),
path("app02/", include("app02.urls")),
]

include(("app name: urls", "app name"))


Example:

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


Use the same route alias in app01/urls.py.

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


Use namespace in views.py, syntax as follows:

reverse("app name: route alias")


Example:

return redirect(reverse("app01:login"))


Use namespace in HTML files of templates, syntax as follows:

{% url "app name: route alias" %}


Example:

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

❮ Django Auth Django Cookie Session ❯