Django Tutorial Part 2 Adding views to url path

Hello Django Forums,

In order to get better at Django I am going through the Django tutorial polls app so that I can better understand the fundamentals. I have a question about the following code presented in the tutorial.

from django.urls import path

from . import views

urlpatterns = [
    # ex: /polls/
    path("", views.index, name="index"),
    # ex: /polls/5/
    path("<int:question_id>/", views.detail, name="detail"),
    # ex: /polls/5/results/
    path("<int:question_id>/results/", views.results, name="results"),
    # ex: /polls/5/vote/
    path("<int:question_id>/vote/", views.vote, name="vote"),
]

When somebody requests a page from your website – say, “/polls/34/”, Django will load the mysite.urls Python module because it’s pointed to by the ROOT_URLCONF setting. It finds the variable named urlpatterns and traverses the patterns in order. After finding the match at 'polls/' , it strips off the matching text ("polls/" ) and sends the remaining text – "34/" – to the ‘polls.urls’ URLconf for further processing. There it matches '<int:question_id>/' , resulting in a call to the detail() view like so:

detail(request=, question_id=34)

The question_id=34 part comes from <int:question_id>. Using angle brackets “captures” part of the URL and sends it as a keyword argument to the view function. The question_id part of the string defines the name that will be used to identify the matched pattern, and the int part is a converter that determines what patterns should match this part of the URL path. The colon (:) separates the converter and pattern name.

I want to walk through what is going on during this explanation and see if I understand this correctly.

Basically /polls/34/ is requested from the user. Django will go to the projects urls because in settings.py we told django to look in that location.

It finds the variable named urlpatterns and goes through each path one by one. It finds the ‘polls/’ path and than strips off the ‘polls/’ portion of /polls/34 and leaves the 34/ left. It sends that to the urls.py for the app which in this case would be polls.urls. for further processing.

Which is shown in this code snippit below.

from django.urls import path

from . import views

urlpatterns = [
    # ex: /polls/
    path("", views.index, name="index"),
    # ex: /polls/5/
    path("<int:question_id>/", views.detail, name="detail"),
    # ex: /polls/5/results/
    path("<int:question_id>/results/", views.results, name="results"),
    # ex: /polls/5/vote/
    path("<int:question_id>/vote/", views.vote, name="vote"),
]

First Question:
Once there, and this is where I get hazy, it goes down the urls path’s and finds int:question_id/ and runs the detail view. Is this because this is the first thing it runs into that has the int:question_id? I observe that it is at the top of the list.

Second Question
The rest of the request that was not stripped off the 34/ seems to have activated the detail view. And I am thinking that it assigns question_id a value of the number that the user requested in their url 34.

I think I am off on a detail. Where am I off?

This process is described in detail at URL dispatcher | Django documentation | Django.