Disable Admin-Style Browsable Interface of Django REST Framework
Last Updated :
16 Aug, 2024
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',
]
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.
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.
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.
Similar Reads
Browsable API in Django REST Framework The browsable API feature in the Django REST framework generates HTML output for different resources. It facilitates interaction with RESTful web service through any web browser. To enable this feature, we should specify text/html for the Content-Type key in the request header. It helps us to use we
8 min read
Adding Permission in API - Django REST Framework There are many different scenarios to consider when it comes to access control. Allowing unauthorized access to risky operations or restricted areas results in a massive vulnerability. This highlights the importance of adding permissions in APIs. Â Django REST framework allows us to leverage permissi
7 min read
Function based Views - Django Rest Framework Django REST Framework allows us to work with regular Django views. It facilitates processing the HTTP requests and providing appropriate HTTP responses. In this section, you will understand how to implement Django views for the Restful Web service. We also make use of the @api_view decorator. Before
13 min read
Integrating Django with Reactjs using Django REST Framework In this article, we will learn the process of communicating between the Django Backend and React js frontend using the Django REST Framework. For the sake of a better understanding of the concept, we will be building a Simple Task Manager and go through the primary concepts for this type of integrat
15+ min read
Concrete GenericAPIViews in Django Rest Framework. Django Rest Framework (DRF) provides powerful tools for building RESTful APIs in Django projects. One of the key components of DRF is Concrete GenericAPIViews, which offer pre-implemented views for common CRUD (Create, Read, Update, Delete) operations. In this article, we will see Concrete GenericAP
2 min read
How to Create a basic API using Django Rest Framework ? Django REST Framework (DRF) is a powerful extension of Django that helps you build APIs quickly and easily. It simplifies exposing your Django models as RESTfulAPIs, which can be consumed by frontend apps, mobile clients or other services.Before creating an API, there are three main steps to underst
4 min read