Exploring URL mapping detail
We briefly mentioned URL maps earlier in the Processing a request section. Django does not automatically know which view function should be executed when it receives a request for a particular URL. The role of URL mapping is to build a link between a URL and a view. For example, in Bookr, you might want to map the /books/
URL to a books_list
view that you have created.
The URL-to-view mapping is defined in the file that Django automatically created called urls.py
, inside the bookr
package directory (although a different file can be set in settings.py
; there’ll be more on that later).
This file contains a variable, urlpatterns
, which is a list of paths that Django evaluates in turn until it finds a match for the URL being requested. The match will either resolve to a view function or another urls.py
file, also containing a urlpatterns
variable, which will be resolved in the same manner. URL files can be chained in this manner for as long as...