Routing and URLs

Routing & URLs in Django Rest Framework

In this section, we will learn how to connect our API views to the URLs so that users can access the API endpoints through the browser or other API tools (like Postman).

Introduction to Django URLs

In Django, every webpage or API endpoint must be linked to a URL path.
We define these URL patterns using something called urls.py.In Django projects, there are typically two levels of URL configuration:

  1. Project-level URL configuration → Usually found in projectname/urls.py (like bookstore/urls.py)
  2. App-level URL configuration → Found in each app (like books/urls.py)

Setting up API Endpoints with urlpatterns

To make an API accessible through the browser or tools like Postman, we need to map the ViewSet (like BookViewSet) to a URL.

Normally, we use a list called urlpatterns to define each path manually. But for ViewSets, there’s a better way — using DRF’s routers.

Using DRF’s DefaultRouter

The DefaultRouter provided by DRF automatically handles the creation of all standard CRUD (Create, Read, Update, Delete) API routes for a ViewSet. It saves us from writing all the paths manually.

Let’s now look at the code step-by-step.

1. books/urls.py – App-Level Routing

This file is created inside your app folder (books/).

 Purpose:
To define API routes specific to your app, like /books/, /books/1/, etc.

 Why we write code here:
We want to keep routing for each app separate and modular, so it’s easy to manage and scale later. If you create more apps like students, orders, etc., each one can have its own urls.py.

Below is a folder Structure for the books/ urls.py file in DRF Project:

├── books/                  ← App Folder (your Django app)

│   ├── migrations/

│   ├── __init__.py

│   ├── admin.py

│   ├── apps.py

│   ├── models.py

│   ├── serializers.py

│   ├── tests.py

│   ├── views.py

│   └── urls.py             ← App-Level Routing File

└── manage.py               ← Django Management Script

# books/urls.py

from django.urls import path, include

from rest_framework.routers import DefaultRouter

from .views import BookViewSet

router = DefaultRouter()

router.register(‘books’, BookViewSet)

urlpatterns = [

    path(”, include(router.urls)),

]

Explanation:

 

 This line imports the required Django methods for defining URL patterns.

// from rest_framework.routers import DefaultRouter

 We import DefaultRouter from DRF to automate URL routing for ViewSets.

// from .views import BookViewSet

 We import our BookViewSet from the views.py file so that it can be linked to the URL.

// router = DefaultRouter()

 We create an instance of DefaultRouter, which will generate routes for our API.

// router.register(‘books’, BookViewSet)

 We are telling the router:

  • books” → is the URL path (this becomes api/books/)
  • BookViewSet → is the view that will handle the requests for this endpoint.

Now, all the following routes will be created automatically:

  • GET /api/books/ → List all books
  • POST /api/books/ → Add a new book
  • GET /api/books/1/ → Get a single book by ID
  • PUT /api/books/1/ → Update a book
  • DELETE /api/books/1/ → Delete a book

urlpatterns = [

    path(”, include(router.urls)),

]

This line means: include all the URLs generated by the router in our app’s URL configuration.

2. bookstore/urls.py – Project-Level Routing

This file is created in your main project folder (bookstore/) — the one with settings.py.

 Purpose:
To include app-level URLs in the main project so Django knows where to find all routes when a request comes in.

 Why we write code here:
This file connects the project to the app-level routes. Without this, Django doesn’t know that your books app has its own APIs.

Below is a folder Structure for the bookstore/ urls.py file in DRF Project:

bookstore/                   ← Project Root Directory

├── bookstore/              ← Project Main Folder (contains settings, main urls, etc.)

│   ├── __init__.py

│   ├── asgi.py

│   ├── settings.py

│   ├── urls.py             ←  Project-Level Routing File

│   └── wsgi.py

# bookstore/urls.py

from django.contrib import admin

from django.urls import path, include

 

urlpatterns = [

    path(‘admin/’, admin.site.urls),

    path(‘api/’, include(‘books.urls’)),

]

Explanation:

from django.contrib import admin

 

 Imports Django’s admin functionality.

from django.urls import path, include

 

 Imports required methods for URL routing.

urlpatterns = [

    path(‘admin/’, admin.site.urls),

 

 Makes the admin panel available at /admin/.

   path(‘api/’, include(‘books.urls’)),

]

 

 This line means:

  • When someone goes to /api/, Django will look at the books/urls.py file for further routing.
  • So, the full path becomes: /api/books/ for accessing the book APIs.

Now here we have listed our Book details on following endpoint :
http://127.0.0.1:8000/api/books/ 

Note:Ensure to hit the POST request to add your Book details. 

After sending post request ,you can see your listed Book on the same endpoint above as:

Summary :

  • urls.py helps map your API to a URL so others can access it.
  • DefaultRouter() makes your life easy by creating all standard CRUD routes automatically.
  • You don’t have to manually write paths like path(‘books/’, BookViewSet.as_view()), DRF handles it behind the scenes.

Exercise:

  • Use DefaultRouter to register your StudentViewSet.

  • Visit the URL http://127.0.0.1:8000/api/students/ in the browser and check if data is shown.

  • Check if /students/1/ returns data for a specific student.

Course Video in English