← Back to Django Course | Chapter 2: Apps, URLs & Views | Lesson 4 of 10

URL Configuration Basics

URLconf is Django's map — it tells the website which page to show when someone types a certain address into their browser.

What a URLconf Is

A URLconf is a Python module containing a list named urlpatterns. Each entry maps a URL pattern (as a string) to a view function that should handle requests matching it.

Example: What a URLconf Is

A URLconf is a Python module containing a list named urlpatterns. Each entry maps a URL pattern (as a string) to a view function that should handle requests matching it.

markup
from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
    path('about/', views.about, name='about'),
]
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

ROOT_URLCONF Setting

settings.py has a ROOT_URLCONF value telling Django which module contains the top-level urlpatterns list. This is usually the urls.py generated in your project folder by startproject.

Example: ROOT_URLCONF Setting

settings.py has a ROOT_URLCONF value telling Django which module contains the top-level urlpatterns list. This is usually the urls.py generated in your project folder by startproject.

markup
ROOT_URLCONF = 'myproject.urls'
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}

Matching Order Matters

Django walks urlpatterns from top to bottom and stops at the first pattern that matches the requested path. A broad pattern placed too early can accidentally swallow requests meant for a more specific one below it.

Note: Put more specific paths above more general ones.

Example: Matching Order Matters

Django walks urlpatterns from top to bottom and stops at the first pattern that matches the requested path. A broad pattern placed too early can accidentally swallow requests meant for a more specific one below it.

markup
urlpatterns = [
    path('blog/latest/', views.latest_post),
    path('blog/<int:pk>/', views.post_detail),
]
{# Django-only code -- models.py/views.py/urls.py/settings.py snippets, or template markup using Django template tags/variables -- can't run standalone via Judge0 or the browser preview, since it needs a real Django project. Only this course's pure-Python examples (example_lang == 'python', no Django imports) are actually runnable, so those still get the button below. #}
Common Mistakes
  1. Forgetting the trailing comma after a path() entry, causing a Python syntax error in urls.py.
  2. Writing view functions that don't match what urls.py actually imports, causing an ImportError.
  3. Assuming URL order doesn't matter — Django matches patterns top to bottom and stops at the first match.
Chapter Summary
  • urlpatterns is a Python list of path() entries mapping URL strings to view functions.
  • Django checks urlpatterns top-to-bottom and uses the first pattern that matches the request path.
  • The root URLconf is pointed to by the ROOT_URLCONF setting in settings.py.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.