What happens in urls.py?

In many ways, urls.py is the entry point for your project. It is usually the first file I open when I study a Django project. It is like reading a map before exploring a terrain. Essentially, urls.py contains the root URL configuration or URLConf of the entire project.

It is a Python list of patterns assigned to a global variable called urlpatterns. Each incoming URL is matched with each pattern from top to bottom in a sequence. In the first match, the search stops, and the request is sent to the corresponding view.

Here is an excerpt of urls.py from python.org, which is built in Django:

urlpatterns = [ 
    # Homepage 
    url(r'^$', views.IndexView.as_view(), name='home'), 
 # About url(r'^about/$', TemplateView.as_view(template_name="python/about.html"), ...

Get Django Design Patterns and Best Practices - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.