0
0
Djangoframework~5 mins

Browsable API interface in Django

Choose your learning style9 modes available
Introduction

The browsable API interface lets you explore and test your API easily in a web browser without extra tools.

You want to quickly check how your API works during development.
You want to let teammates or testers try the API without writing code.
You want a simple way to see API responses and send requests interactively.
You want to debug API endpoints by sending different data easily.
You want to document your API with a user-friendly interface.
Syntax
Django
In Django REST Framework, enable the browsable API by including 'rest_framework' in INSTALLED_APPS and using APIView or ViewSets with appropriate serializers.
The browsable API is enabled by default when you use Django REST Framework's views.
It works best with proper serializers and authentication set up.
Examples
Add 'rest_framework' to your Django project settings to enable REST Framework features including the browsable API.
Django
INSTALLED_APPS = [
    ...
    'rest_framework',
]
A simple APIView that returns a JSON message. When browsed in a browser, it shows a nice form and response.
Django
from rest_framework.views import APIView
from rest_framework.response import Response

class HelloView(APIView):
    def get(self, request):
        return Response({"message": "Hello, world!"})
Using routers and viewsets automatically provides browsable API pages for your models.
Django
from rest_framework import routers, serializers, viewsets

# Define serializer and viewset for a model

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

# Include router.urls in your urls.py
Sample Program

This example creates a simple API endpoint at /greet/ that returns a greeting message. When you visit this URL in a browser, you see the browsable API interface with a GET button and the JSON response.

Django
from django.urls import path
from rest_framework.views import APIView
from rest_framework.response import Response

class GreetingView(APIView):
    def get(self, request):
        return Response({"greeting": "Hello from browsable API!"})

urlpatterns = [
    path('greet/', GreetingView.as_view(), name='greet'),
]
OutputSuccess
Important Notes

The browsable API helps you test your API without extra tools like Postman.

It respects your API's authentication and permissions, so you see only allowed data.

For production, you might want to disable or limit the browsable API for security.

Summary

The browsable API is a web interface to explore and test your API easily.

It is enabled by default in Django REST Framework when you use its views.

It helps beginners and teams understand and debug APIs without writing extra code.