Open In App

Disable Admin-Style Browsable Interface of Django REST Framework

Last Updated : 16 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Django REST Framework (DRF) is a powerful and flexible toolkit for building Web APIs in Django. One of its features is the browsable API, which provides a user-friendly, web-based interface for interacting with the API. While this can be helpful during development and debugging, it may not be desirable in a production environment for security or performance reasons. In this article, we will walk through the steps to disable the admin-style browsable interface of the Django REST Framework.

How to Disable Admin-Style Browsable Interface of Django REST Framework?

Step 1: Set Up a Django Project

First, we need to create a Django project and a Django app. You can skip this part if you already have a project set up. Otherwise, follow these steps:

Install Django and Django REST Framework:

pip install django djangorestframework

Create a Django Project:

django-admin startproject myproject
cd myproject

Create a Django App:

python manage.py startapp myapp

Add the App to Installed Apps:

Open myproject/settings.py and add 'myapp' and 'rest_framework' to the INSTALLED_APPS list:

INSTALLED_APPS = [
...
'rest_framework',
'myapp',
]
iop


Create a Simple Model:

Open myapp/models.py and create a simple model:

Python
from django.db import models

class Item(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()

    def __str__(self):
        return self.name

Create and Apply Migrations:

python manage.py makemigrations
python manage.py migrate

Create a Superuser:

python manage.py createsuperuser

Create a Serializer:

Open myapp/serializers.py and create a serializer for the Item model:

Python
from rest_framework import serializers
from .models import Item

class ItemSerializer(serializers.ModelSerializer):
    class Meta:
        model = Item
        fields = '__all__'

Create a ViewSet:

Open myapp/views.py and create a viewset for the Item model:

Python
from rest_framework import viewsets
from .models import Item
from .serializers import ItemSerializer

class ItemViewSet(viewsets.ModelViewSet):
    queryset = Item.objects.all()
    serializer_class = ItemSerializer

Configure URLs:

Open myapp/urls.py and configure the URLs for the API:

Python
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import ItemViewSet

router = DefaultRouter()
router.register(r'items', ItemViewSet)

urlpatterns = [
    path('', include(router.urls)),
]

Open myproject/urls.py and include the app URLs:

Python
from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('admin/', admin.site.urls),
    path('api/', include('myapp.urls')),
]

Run the Server:

python manage.py runserver

At this point, you should be able to visit http://127.0.0.1:8000/api/items/ and see the browsable API interface.

admin

Step 2: Disable the Browsable API

Before Disable Admin-Style Browsable Interface:

To disable the browsable API, we need to modify the settings in myproject/settings.py. Specifically, we need to change the DEFAULT_RENDERER_CLASSES setting in the REST_FRAMEWORK configuration.

Modify settings.py:

Open myproject/settings.py and add or update the REST_FRAMEWORK settings:

REST_FRAMEWORK = {
'DEFAULT_RENDERER_CLASSES': (
'rest_framework.renderers.JSONRenderer',
),
}

Restart the Server:

python manage.py runserver

After Disable Admin-Style Browsable Interface:

After making this change, if you visit http://127.0.0.1:8000/api/items/, you will no longer see the browsable API interface. Instead, you will get a JSON response directly.

admin1

Conclusion

The browsable API in Django REST Framework is an excellent tool for development and debugging, providing a convenient interface for testing and interacting with your API. However, in a production environment, it is often advisable to disable this feature for security and performance reasons. By modifying the DEFAULT_RENDERER_CLASSES setting, you can easily disable the admin-style browsable interface and ensure that your API only returns JSON responses.


Next Article
Article Tags :
Practice Tags :

Similar Reads