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
Routing APIs with DRF0%
Study Materials
Django for Backend DevelopmentProgress 0%
1.1Installing Django & Environment Setup1.2Django Project Structure1.3Creating First Django App
6.1Serializers6.2APIView & ViewSet6.3Routing APIs with DRF
HomeTutorialsDjango for Backend DevelopmentRouting APIs with DRF
Custom Middleware

Routing APIs with DRF

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

When working with Django REST Framework ViewSets, manually writing individual path() entries for every CRUD endpoint (list, create, retrieve, update, destroy) is tedious and error-prone. DRF Routers automatically generate standardized, consistent RESTful URL routing schemes, including trailing slash management and the browsable API root.


1. How DRF Routers Work

A DRF router inspects a ViewSet and automatically generates the complete set of standard REST URL patterns:

1ArticleViewSet registered at 'articles':
2GET /api/v1/articles/ ──► ArticleViewSet.list()
3POST /api/v1/articles/ ──► ArticleViewSet.create()
4GET /api/v1/articles/{slug}/ ──► ArticleViewSet.retrieve()
5PUT /api/v1/articles/{slug}/ ──► ArticleViewSet.update()
6PATCH /api/v1/articles/{slug}/ ──► ArticleViewSet.partial_update()
7DELETE /api/v1/articles/{slug}/ ──► ArticleViewSet.destroy()

2. Configuring DefaultRouter vs SimpleRouter

DRF provides two main router classes:

  • SimpleRouter: Generates only the basic CRUD URL patterns.
  • DefaultRouter: Extends SimpleRouter by adding a default API Root endpoint displaying hyperlinks to all registered resources, as well as optional .json / format suffix patterns.
1# articles/urls.py
2from django.urls import path, include
3from rest_framework.routers import DefaultRouter
4from .views import ArticleViewSet, CategoryViewSet
5 
6# 1. Instantiate the router
7router = DefaultRouter()
8 
9# 2. Register viewsets with URL prefixes
10router.register(r"articles", ArticleViewSet, basename="article")
11router.register(r"categories", CategoryViewSet, basename="category")
12 
13# 3. Include router.urls in urlpatterns
14app_name = "articles_api"
15 
16urlpatterns = [
17 path("", include(router.urls)),
18]

Visiting http://127.0.0.1:8000/api/v1/ renders DRF's interactive Browsable API Root with clickable links to /articles/ and /categories/!


3. When is basename Required?

If a ViewSet has a queryset attribute, DRF automatically derives the basename from the model name:

1class ArticleViewSet(ModelViewSet):
2 queryset = Article.objects.all() # DRF automatically uses basename="article"

However, if your ViewSet overrides get_queryset() and omits the static queryset attribute, you must specify basename:

1router.register(r"my-articles", MyArticlesViewSet, basename="my-article")

4. Nested Routers with drf-nested-routers

For hierarchical REST APIs (e.g. /articles/{article_id}/comments/{comment_id}/), use the drf-nested-routers package:

1from rest_framework_nested import routers
2from .views import ArticleViewSet, CommentViewSet
3 
4# Parent router
5router = routers.SimpleRouter()
6router.register(r"articles", ArticleViewSet)
7 
8# Nested child router
9articles_router = routers.NestedSimpleRouter(router, r"articles", lookup="article")
10articles_router.register(r"comments", CommentViewSet, basename="article-comments")
11 
12urlpatterns = [
13 path("", include(router.urls)),
14 path("", include(articles_router.urls)),
15]

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What is the primary advantage of using DRF Routers with ViewSets?
Question 2
What extra feature does DefaultRouter offer over SimpleRouter?
Question 3
When is the basename parameter strictly mandatory when calling router.register()?
Question 4
How does DRF's router construct the named URL for a retrieve action on a viewset registered with basename="article"?
Question 5
What package provides clean URL routing for nested endpoints like /articles/{id}/comments/?
Next Lesson

Custom Middleware

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

Related Lessons

Previous LessonNext Lesson
APIView & ViewSetCustom Middleware

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