URL Routing
The URL is the route: the path (the 1 line, the view), the name (the reverse, the {% url %}), and the 2 level (the project the include, the app the path). The converter is the capture (the int, the str, the slug, the uuid, the path), and the re_path is the regex (the no the converter the no). The 404 is the no the match, the 400 is the bad URL, and the redirect is the trailing slash (the APPEND_SLASH the auto). This chapter covers the path (the 6 the converter), the 2 level (the include, the prefix), the name (the reverse, the no the hardcode), and the 404 the 400 the redirect.
The path: The 6 converter
# the CONVERTER (the 6, the capture's):
from django.urls import path
urlpatterns = [
# the int (the 1, the 2, the no
# the "abc", the no the "-1"):
path("notes/<int:pk>/", note_detail),
# the str (the no the /, the
# the default, the no the name):
path("notes/<str:slug>/", note_by_slug),
# the slug (the x-y-z, the no
# the space, the no the -):
path("notes/<slug:slug>/", note_by_slug),
# the uuid (the 8-4-4-4-12, the
# the no the format):
path("api/<uuid:note_id>/", api_note),
# the path (the the /, the the
# the all, the the no):
path("files/<path:filename>/",
file_serve),
# the name (the reverse, the
# the {% url %} the template):
path("notes/<int:pk>/edit/",
note_edit, name="note-edit"),
]
# the the no: the int the "1" (the
# the "abc" the no, the 404)
# the the no: the name the reverse
# (the hardcode the no, the the
# the URL the no the 1 the place)
The path is the 1 line: the <int:pk> is the capture (the 1, the 2, the no the "abc", the no the "-1"), the <str:slug> is the no the /, the <slug:slug> is the x-y-z (the no the space), the <uuid:note_id> is the format (the no the bad), and the <path:filename> is the the the / (the all, the no the match the 1). The name is the reverse: the {% url "note-detail" 1 %} is the URL (the no the hardcode, the the the URL the 1 the place, the the the change the the the template the no), and the reverse is the Python (the reverse("note-detail", args=[1]) the redirect) - the 2 level is the next: the project the include, the app the path, the prefix the the the the the the.
The 2 level and the 404
# the 2 LEVEL (the project the include):
# the project: notes/urls.py
from django.urls import path, include
urlpatterns = [
path("admin/", admin.site.urls),
path("notes/", include(
"notes_app.urls")), # the
# prefix
path("api/", include(
"api.urls")), # the
# the 2
# the no the name (the the
# the 2 the app's the same
# the name, the no the
# the):
]
# the /notes/ the app's, the
# /notes/1/ the detail, the
# /api/ the api's, the /admin/
# the admin's
# the 404 (the no match, the page):
# the 404.html (the no the
# the template, the no the
# the):
# <!doctype html><html>
# <body><h1>404</h1>
# <p>the note the no</p>
# </body></html>
# the REDIRECT (the trailing, the
# APPEND_SLASH):
# the /notes (the no the /) the
# the 301 the /notes/ (the
# the auto, the no the 404)
# the the no: the API (the no the
# the slash, the no the 301, the
# the no the):
# the: the
# APPEND_SLASH = False (the
# the API's the no the 301)
The 2 level is the include: the project the prefix (the "notes/" the app), the app the path (the "" the list, the <int:pk> the detail), and the no the name (the 2 the app the same the name, the no the the the the the the) - the /notes/ is the list, the /notes/1/ is the detail, the /api/ is the api, and the /admin/ is the admin. The 404 is the no the match: the 404.html is the template (the no the the the the the the, the the the no the the), the 400 is the bad URL (the no the the), and the redirect is the trailing slash (the APPEND_SLASH the auto, the /notes the 301 the /notes/, the no the 404) - the no the API (the no the slash, the no the 301, the no the the the the) - the rule is the one: the path the 6 the converter, the 2 level the include, the name the reverse, and the 404 the template - the next chapter's view is the request the response the function the 1 the line.
The Working Rules
- The path is the 1 line, the 6 converter. The int the 1 the 2 (the no the abc, the no the -1), the str the no the /, the slug the x-y-z, and the uuid the format (the no the bad) - the path the / (the all, the no the 1), and the name the reverse (the {% url %}, the no the hardcode).
- The 2 level is the include, the prefix the project. The project the notes/ (the app's prefix), and the app the path (the empty the list, the int:pk the detail) - the no the name (the 2 the app's the same, the no the conflict), and the /notes/ the list (the /notes/1/ the detail, the /api/ the api's).
- The name is the reverse, the no the hardcode. The {% url "note-detail" 1 %} the template, and the reverse the Python (the redirect, the no the string) - the URL the 1 place (the urls.py, the no the template), and the name the no the path (the change the no the 200 template's break).
- The 404 is the template, the APPEND_SLASH the API's no. The 404.html the no the default (the custom, the no the plain), and the /notes the 301 the /notes/ (the auto, the no the 404) - the API the no the slash (the APPEND_SLASH = False, the no the 301), and the 400 the bad URL (the no the view, the no the 500).
Summary
The URL is the route: the path the 1 line (the 6 converter: the int, the str, the slug, the uuid, the path, and the no the name), the 2 level the include (the project the prefix, the app the path, the no the name the conflict), and the name the reverse (the {% url %} the template, the reverse the Python, the no the hardcode, the URL the 1 place). The 404 is the no the match (the 404.html the template, the custom the no the plain), the 400 is the bad URL, and the redirect is the trailing slash (the APPEND_SLASH the auto, the /notes the 301 the /notes/, the API the no the slash the False) - the next chapter's view is the request the response: the function the 1 line, the GET the POST, the JsonResponse the HTML, and the decorator the no the login the 403.