URL Configuration Basics
In this page:
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.
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.
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.
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. #}
- Forgetting the trailing comma after a path() entry, causing a Python syntax error in urls.py.
- Writing view functions that don't match what urls.py actually imports, causing an ImportError.
- Assuming URL order doesn't matter — Django matches patterns top to bottom and stops at the first match.
- 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.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: