MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
URL Dispatcher and Routing0%
Study Materials
Django for Backend DevelopmentProgress 0%
1.1Installing Django & Environment Setup1.2Django Project Structure1.3Creating First Django App
5.1Function-Based Views (FBV)5.2Class-Based Views (CBV)5.3URL Dispatcher and Routing
HomeTutorialsDjango for Backend DevelopmentURL Dispatcher and Routing
Serializers

URL Dispatcher and Routing

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study MaterialsCheatsheet

The URL Dispatcher is the front door of every Django application. It matches incoming HTTP request paths against a list of URL patterns, extracts dynamic parameters (like integers, slugs, or UUIDs) using Path Converters, and dispatches the request to the matching view callable.


1. Anatomy of path() and re_path()

1from django.urls import path, re_path
2from . import views
3 
4urlpatterns = [
5 # 1. Static literal path
6 path("about/", views.about_view, name="about"),
7 
8 # 2. Dynamic path with path converter
9 path("articles/<int:pk>/", views.article_by_id, name="by-id"),
10 
11 # 3. Dynamic slug path
12 path("articles/<slug:slug>/", views.article_by_slug, name="by-slug"),
13 
14 # 4. Regex path for specialized formats (e.g. 4-digit year)
15 re_path(r"^archive/(?P<year>[0-9]{4})/$", views.archive_view, name="archive"),
16]

2. Built-in Path Converters

Django includes five native path converters that both match patterns and typecast extracted URL parameters into native Python types:

Path ConverterMatchesPython Type Passed to ViewExample
str (default)Any non-empty string excluding /str<str:username> matches "alice"
intZero or any positive integerint<int:pk> matches 42 -> passes 42
slugASCII letters, numbers, hyphens, underscoresstr<slug:post_slug> matches "django-orm-guide"
uuidFormatted UUID stringuuid.UUID<uuid:id> matches "123e4567-e89b-..."
pathAny non-empty string including /str<path:filepath> matches "media/docs/invoice.pdf"

3. Creating Custom Path Converters

If standard converters do not fit your domain (e.g. 4-digit years or phone numbers), register a custom converter:

1# config/converters.py
2class FourDigitYearConverter:
3 regex = r"[0-9]{4}"
4 
5 def to_python(self, value: str) -> int:
6 # Cast matched URL string into Python integer
7 return int(value)
8 
9 def to_url(self, value: int) -> str:
10 # Cast Python value back to URL string for reverse()
11 return f"{value:04d}"

Register in urls.py:

1# config/urls.py
2from django.urls import path, register_converter
3from config.converters import FourDigitYearConverter
4from . import views
5 
6# Register converter with tag name 'yyyy'
7register_converter(FourDigitYearConverter, "yyyy")
8 
9urlpatterns = [
10 # Clean and readable!
11 path("archive/<yyyy:year>/", views.year_archive, name="year-archive"),
12]

4. Namespacing and URL Resolution

Always define app_name inside your app's urls.py:

1# billing/urls.py
2app_name = "billing"
3 
4urlpatterns = [
5 path("invoices/<int:pk>/", views.invoice_detail, name="invoice-detail"),
6]

Resolve URLs unambiguously across your project:

  • In Python: reverse("billing:invoice-detail", kwargs={"pk": 105})
  • In Templates: {% url 'billing:invoice-detail' pk=invoice.id %}

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What Python data type does the <int:pk> path converter pass into the view function?
Question 2
What characters are matched by the <slug:slug> path converter?
Question 3
What two methods must a custom path converter class implement?
Question 4
When should you use the <path:subpath> converter instead of <str:subpath>?
Question 5
What function registers a custom path converter with Django's URL dispatcher?
Next Lesson

Serializers

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
Class-Based Views (CBV)Serializers

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master Django for Backend Development with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext