In Django Rest Framework, you can customize routing by defining custom URL patterns that map to views. You can do this by defining a file in your app’s directory and using the url() function to define URL patterns that map to views.

Here is an example of how to define a custom route for a view in Django Rest Framework:

# app/
from django.urls import path
from .views import MyView

urlpatterns = [
    path('my-view/', MyView.as_view(), name='my-view'),
    path('my-view/<int:pk>/', MyView.as_view(), name='my-view-detail'),

In this example, we define two URL patterns for the MyView view. The first URL pattern maps to the MyView view at the endpoint /my-view/. The second URL pattern maps to the MyView view at the endpoint /my-view/<int:pk>/, where pk is an integer value that represents the primary key of a model instance.

To define custom routing, you can use the path() function to define URL patterns that map to views, and you can include parameters in the URL pattern to capture dynamic values from the URL.

Once you have defined your custom URL patterns, you need to include them in your app’s main file using the include() function:

# project/
from django.urls import path, include

urlpatterns = [
    path('api/', include('app.urls')),

In this example, we include the app.urls module under the /api/ endpoint in the main file. This will make the custom URL patterns defined in app.urls available at the endpoint /api/.

A Simple Project on Custom Routing in Django Rest FrameWork

First, let’s create a new Django project and app:

# Create a new Django project
$ django-admin startproject customrouting

# Create a new Django app
$ cd customrouting
$ python startapp blog

Next, we need to install Django Rest Framework:

$ pip install djangorestframework

Now, let’s create a simple blog API that allows us to create, update, and view blog posts. We’ll define two URL patterns: one for creating and updating blog posts, and another for viewing all blog posts.

In blog/, define the following URL patterns:

from django.urls import path
from .views import BlogPostDetailView, BlogPostListView

urlpatterns = [
    path('blog-posts/', BlogPostListView.as_view(), name='blog-post-list'),
    path('blog-posts/<int:pk>/', BlogPostDetailView.as_view(), name='blog-post-detail'),

Here, we’ve defined two URL patterns: one for the BlogPostListView view, which lists all blog posts, and another for the BlogPostDetailView view, which shows the details of a single blog post.

Next, let’s define the views. In blog/, define the following views:

from rest_framework import generics
from .models import BlogPost
from .serializers import BlogPostSerializer

class BlogPostListView(generics.ListCreateAPIView):
    queryset = BlogPost.objects.all()
    serializer_class = BlogPostSerializer

class BlogPostDetailView(generics.RetrieveUpdateDestroyAPIView):
    queryset = BlogPost.objects.all()
    serializer_class = BlogPostSerializer

In these views, we’ve used Django Rest Framework’s generic views to handle the CRUD operations for the BlogPost model. We’ve also defined a serializer that converts the BlogPost model to and from JSON.

Finally, let’s define the BlogPost model and serializer. In blog/, define the following model:

from django.db import models

class BlogPost(models.Model):
    title = models.CharField(max_length=255)
    content = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    def __str__(self):
        return self.title

And in blog/, define the following serializer:

from rest_framework import serializers
from .models import BlogPost

class BlogPostSerializer(serializers.ModelSerializer):
    class Meta:
        model = BlogPost
        fields = ('id', 'title', 'content', 'created_at', 'updated_at')

Now we can run the server and test our API. Start the server with python runserver, and navigate to http://localhost:8000/blog-posts/ to view all blog posts. You can create new blog posts by sending a POST request to http://localhost:8000/blog-posts/, and update or delete existing blog posts by sending a PUT or DELETE request to http://localhost:8000/blog-posts/<id>/.

That’s it! This is a simple example of how to define custom routing in Django Rest Framework. You can build on this project to create more complex APIs with custom URL patterns and views.