Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Question 2 of 9 · Django Views & URL Routing
Interview question

How does Django's URL routing work with path() and re_path()? Django का URL routing path() और re_path() से कैसे काम करता है?

Answer
FunctionPattern syntaxUse case
path()Simple converters like <int:pk>Most common URLs - clean, readable
re_path()Full regular expressionsComplex patterns not expressible with simple converters
# urls.py using path() - preferred for most cases
from django.urls import path
from . import views

urlpatterns = [
    path('books/', views.book_list, name='book-list'),
    path('books/<int:pk>/', views.book_detail, name='book-detail'),
    path('books/<slug:slug>/', views.book_by_slug, name='book-by-slug'),
    path('archive/<int:year>/<int:month>/', views.archive, name='archive'),
]

# Built-in path converters:
# str - matches any non-empty string, excluding '/' (default if omitted)
# int - matches positive integers
# slug - matches letters, numbers, hyphens, underscores
# uuid - matches a formatted UUID
# path - matches any string, INCLUDING '/'

# re_path() - for patterns path() converters can't express
from django.urls import re_path

urlpatterns += [
    re_path(r'^articles/(?P<year>[0-9]{4})/$', views.year_archive),
    re_path(r'^books/(?P<isbn>\d{3}-\d{10})/$', views.book_by_isbn),
]

# Accessing captured URL parameters in the view
def book_detail(request, pk):  # 'pk' matches the <int:pk> converter name
    book = get_object_or_404(Book, pk=pk)
    return render(request, 'book_detail.html', {'book': book})

# Custom path converters for reusable patterns
class FourDigitYearConverter:
    regex = '[0-9]{4}'
    def to_python(self, value):
        return int(value)
    def to_url(self, value):
        return '%04d' % value

from django.urls import register_converter
register_converter(FourDigitYearConverter, 'yyyy')
urlpatterns += [path('archive/<yyyy:year>/', views.year_archive)]
FunctionPattern syntaxUse case
path()Simple converters <int:pk>ज़्यादातर URLs
re_path()Full regular expressionsComplex patterns
from django.urls import path
from . import views

urlpatterns = [
    path('books/', views.book_list, name='book-list'),
    path('books/<int:pk>/', views.book_detail, name='book-detail'),
    path('books/<slug:slug>/', views.book_by_slug, name='book-by-slug'),
]

# Built-in path converters: str, int, slug, uuid, path

from django.urls import re_path

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

def book_detail(request, pk):
    book = get_object_or_404(Book, pk=pk)
    return render(request, 'book_detail.html', {'book': book})

# Custom path converter
class FourDigitYearConverter:
    regex = '[0-9]{4}'
    def to_python(self, value):
        return int(value)
    def to_url(self, value):
        return '%04d' % value

from django.urls import register_converter
register_converter(FourDigitYearConverter, 'yyyy')
urlpatterns += [path('archive/<yyyy:year>/', views.year_archive)]

Was this answer clear?