URL dispatcher – urls.py
The URL dispatcher is one of the key concepts in the Django architecture. The URL dispatcher maps HTTP requests to the view functions (or classes) specified in the URL patterns that are written in the urls.py file. You may not understand what this means if you are new to the concept. We'll explain the concept step by step.
The urls.py file
Let's open the urls.py file that is created when you start a Django project by running the startproject command. You can see that some code is already written in the file.
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
from ... import ...
This syntax is used when you want to use Python modules in the file. In this case, the admin module is imported from the django.contrib file, and the path module is imported from the django.urls file.
URL patterns
URL patterns are used for defining URLs for incoming HTTP requests and paths for the functions or classes to handle the requests. In this case, 'admin/' is a URL for an HTTP request, and 'admin.site.urls
' is a function used to handle the HTTP request.
When the server is running, type 'localhost:8000/admin/'. You can see the login view for the Django admin site as shown below.
At this point, you cannot log in as no users have been created yet. We'll explain how to create a user to log in to the Django admin site later.