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 Routing0%
Study Materials
REST API Development with DjangoProgress 0%
1.1REST Principles1.2HTTP Methods Overview1.3Status Codes
3.1Create, Retrieve, Update, Delete APIs3.2Endpoints for Models3.3URL Routing
HomeTutorialsREST API Development with DjangoURL Routing
Token Authentication

URL Routing

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

DRF DefaultRouter and SimpleRouter Configuration

Django REST Framework routers eliminate manual URL routing boilerplate by automatically mapping standard ViewSet actions to predictable RESTful URLs. Understanding the differences between SimpleRouter and DefaultRouter, customizing trailing slashes, and managing format suffixes ensures seamless API routing.


1. Comparing SimpleRouter vs DefaultRouter

FeatureSimpleRouterDefaultRouter
Generates CRUD routesYes (list, create, retrieve, update, destroy)Yes
API Root Endpoint (/)No (returns 404 at root)Yes (Generates hyperlinked index of all endpoints)
Format Suffix PatternsOptional manual configurationAutomatic (supports .json, .api suffixes)
Sweet SpotSub-routers or internal micro-APIsPublic API roots and browsable enterprise portals

2. Advanced Router Configuration

1# api/urls.py
2from django.urls import path, include
3from rest_framework.routers import DefaultRouter
4from .views import ProductViewSet, CategoryViewSet, OrderViewSet
5 
6# Instantiate DefaultRouter with optional trailing slash configuration
7router = DefaultRouter(trailing_slash=True) # Enforces standard trailing slashes /items/
8 
9# Register viewsets
10router.register(r"products", ProductViewSet, basename="product")
11router.register(r"categories", CategoryViewSet, basename="category")
12router.register(r"orders", OrderViewSet, basename="order")
13 
14app_name = "api_v1"
15 
16urlpatterns = [
17 # Exposes API Root and all registered viewset paths
18 path("", include(router.urls)),
19]

3. The Trailing Slash Debate (trailing_slash=False)

By default, Django and DRF enforce trailing slashes (/api/v1/products/). If a client requests /api/v1/products, Django sends a 301 Moved Permanently redirect to the slashed URL.

In REST APIs consumed by frontend frameworks, unexpected 301 redirects on POST requests can drop the request body. If your client architecture strictly avoids trailing slashes:

1# Disables trailing slash requirement: routes match /api/v1/products
2router = DefaultRouter(trailing_slash=False)

4. Inspecting Registered Router URLs

To view every URL pattern generated by your router, inspect router.urls:

1for url in router.urls:
2 print(url.pattern, "──►", url.name)

Output:

1^products/$ ──► product-list
2^products/(?P<pk>[^/.]+)/$ ──► product-detail
3^categories/$ ──► category-list
4^categories/(?P<pk>[^/.]+)/$ ──► category-detail

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What key feature does DefaultRouter include that SimpleRouter lacks?
Question 2
What happens by default if a client issues a POST request to a Django endpoint without a trailing slash (e.g. /api/products)?
Question 3
How do you configure a DRF router to accept URLs without trailing slashes (e.g. /api/v1/products)?
Question 4
When calling router.register("items", ItemViewSet, basename="item"), what is the named URL for the detail view?
Question 5
Can standard path() routes and router-generated routes coexist in the same urlpatterns list?
Next Lesson

Token Authentication

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

Related Lessons

Previous LessonNext Lesson
Endpoints for ModelsToken Authentication

Practice Quiz

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

Mentor-Led Practical Training

Master REST API Development with Django 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