URL Routing0%

URL Routing

Beginner12 min read•Updated: Jul 11, 2026
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

Python
# api/urls.py
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import ProductViewSet, CategoryViewSet, OrderViewSet
 
# Instantiate DefaultRouter with optional trailing slash configuration
router = DefaultRouter(trailing_slash=True) # Enforces standard trailing slashes /items/
 
# Register viewsets
router.register(r"products", ProductViewSet, basename="product")
router.register(r"categories", CategoryViewSet, basename="category")
router.register(r"orders", OrderViewSet, basename="order")
 
app_name = "api_v1"
 
urlpatterns = [
# Exposes API Root and all registered viewset paths
path("", include(router.urls)),
]

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:

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

4. Inspecting Registered Router URLs

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

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

Output:

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

Interactive Quiz

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

Practice Quiz

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

PrevNext