URLconf / urls.py (Django)

A URLconf is Django's URL configuration — the urls.py files that map URL patterns to views using path(). When a request arrives, Django walks the patterns top to bottom and calls the view of the first one that matches.

Django

Routing in Django is explicit: you list URL patterns with path('about/', views.about). The project's root urls.py is the entry point, and it usually delegates to each app's own urls.py with include('blog.urls') to keep things modular.

path() supports typed converters in angle brackets, like path('post/<int:pk>/', views.detail), which capture parts of the URL and pass them to the view as arguments. re_path() exists for full regular-expression matching when path converters are not enough.

Naming a pattern with name='post_detail' lets you reverse it elsewhere with reverse()/{% url %} instead of hard-coding paths, and namespaces (app_name) keep names from colliding across apps.

Syntax

urlpatterns = [path("post/<int:pk>/", views.detail, name="detail")]

Examples

Python

# blog/urls.py
from django.urls import path
from . import views

app_name = "blog"
urlpatterns = [
    path("", views.index, name="index"),
    path("post/<int:pk>/", views.detail, name="detail"),
]

Learn it in a lesson

See also

Back to the Dictionary