๐Ÿ“ Django

URL Routing in Django

P
Author
PyLand Team
๐Ÿ“…
Published
30.06.2026
โฑ๏ธ
Reading time
1 min
๐Ÿ‘๏ธ
Views
253
๐ŸŒฑ
Level
Beginner

Django maps URLs to views via urlpatterns.

Basic Syntax

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

urlpatterns = [
    path('', views.task_list, name='task-list'),
    path('<int:pk>/', views.task_detail, name='task-detail'),
    path('create/', views.create_task, name='task-create'),
    path('<int:pk>/edit/', views.edit_task, name='task-edit'),
    path('<int:pk>/delete/', views.delete_task, name='task-delete'),
]

path() Converters

path('<int:pk>/', view)          # integer โ†’ pk
path('<str:slug>/', view)        # string โ†’ slug
path('<uuid:id>/', view)         # UUID
path('<slug:slug>/', view)       # slug (letters, digits, hyphens)

name= โ€” Named URLs

path('tasks/', views.task_list, name='task-list')

In a template:

<a href="{% url 'task-list' %}">All Tasks</a>
<a href="{% url 'task-detail' pk=task.pk %}">Task #{{ task.pk }}</a>

In Python:

from django.urls import reverse
url = reverse('task-detail', kwargs={'pk': 5})  # '/tasks/5/'
redirect('task-list')

include() โ€” Connecting Application URLs

# mysite/urls.py
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('tasks/', include('tasks.urls')),
    path('blog/', include('blog.urls')),
    path('api/', include('api.urls')),
]

Namespace โ€” URL Namespacing

# tasks/urls.py
app_name = 'tasks'  # โ† add this

urlpatterns = [
    path('', views.task_list, name='list'),
    path('<int:pk>/', views.task_detail, name='detail'),
]

In a template:

{% url 'tasks:list' %}
{% url 'tasks:detail' pk=task.pk %}

re_path โ€” Regular Expressions (rarely needed)

from django.urls import re_path

urlpatterns = [
    re_path(r'^archive/(?P<year>[0-9]{4})/$', views.archive),
]

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

Search in Django

You can start with simple Django ORM filters and move to PostgreSQL features when you...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 279
๐Ÿ“

Swagger Documentation with drf-spectacular

drf-spectacular generates an OpenAPI 3.0 schema and Swagger UI for DRF.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 563
๐Ÿ“

settings.py: Django Configuration

settings.py is the central configuration file for a Django project.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 315
๐ŸŽ“ Continue learning

Courses that cover this material

Visit the course to apply this material in practice.

Django RequestLab: from HTTP to secure production Open course curriculum