urlpatterns is the Django URL-configuration variable: a Python list of path objects that connects incoming URLs with the code handling them. Each path object maps a URL to a view or loads another mapping file, allowing URL definitions to form a hierarchy. section 1.1.1 section 2.6
A Django request follows this route:
- The web server passes the request to Django through the WSGI application. section 1.1.1 section 2.6
- Django checks the URL patterns in urls.py for a match. section 2.6
- When a path matches, Django calls its associated view, passing the HTTP request information. section 1.1.1 section 2.6
Registering a RiffMates route
The central RiffMates/RiffMates/urls.py file can retain the Django Admin route and add the home app's credits view: path("admin/", admin.site.urls) and path('credits/', home_views.credits). Importing the module as home_views keeps similarly named views.py modules distinguishable. URL patterns omit the leading slash because Django supplies it. section 2.6
Listing 2.3 Register your view
# RiffMates/RiffMates/urls.py
from django.contrib import admin
from django.urls import path
from home import views as home_views #1
urlpatterns = [
path("admin/", admin.site.urls),
path('credits/', home_views.credits), #2
]
Use the complete route-registration listing to see the imports, urlpatterns list, existing Admin mapping, and credits view mapping together. section 2.6
