How to add API in a Django Project

Praveen Xavier
2 min readDec 11, 2024

--

You can use Django REST Framework (DRF), which is a powerful toolkit for building Web APIs in Django. Here’s a step-by-step guide on how to integrate DRF into your existing project:

Step 1: Install Django REST Framework

If you haven’t installed DRF yet, you can install it using pip:

pip install djangorestframework

Step 2: Add rest_framework to INSTALLED_APPS

In your Django project’s settings.py, add rest_framework to the INSTALLED_APPS list:

INSTALLED_APPS = [
# other apps...
'rest_framework',
]

Step 3: Create an API View

You can create API views using Django REST Framework’s views. There are multiple ways to create API views, including:

  • Function-based views (FBVs)
  • Class-based views (CBVs)
  • ViewSets

Let’s create a simple API using a class-based view.

1. Create a new file for the API views (if necessary)

Inside one of your apps (or create a new app if needed), create a new file called views.py and define your API view.

Example for a simple model Book in an app called library:

from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework import status
from .models import Book
from .serializers import BookSerializer
class BookList(APIView):
def get(self, request):
books = Book.objects.all()
serializer = BookSerializer(books, many=True)
return Response(serializer.data)
def post(self, request):
serializer = BookSerializer(data=request.data)
if serializer.is_valid():
serializer.save()
return Response(serializer.data, status=status.HTTP_201_CREATED)
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

2. Create a serializer

Django REST Framework uses serializers to convert complex data types (like models) into native Python datatypes (like dictionaries) and vice versa. In the same app as your model, create a serializers.py file:

from rest_framework import serializers
from .models import Book
class BookSerializer(serializers.ModelSerializer):
class Meta:
model = Book
fields = '__all__'

Step 4: Define URLs for the API

You need to define URL routes for the API views. Inside your app, create or modify the urls.py file:

from django.urls import path
from .views import BookList
urlpatterns = [
path('books/', BookList.as_view(), name='book-list'),
]

In your project’s main urls.py, include the app’s URL configurations:

from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('api/', include('library.urls')), # Include the app's URLs
]

Step 5: Migrate the Database (if you haven’t already)

If you added a new model (e.g., Book), you need to run migrations:

python manage.py makemigrations
python manage.py migrate

Step 6: Test the API

Now that your API is set up, you can test it by running the Django development server:

python manage.py runserver

Visit http://127.0.0.1:8000/api/books/ to interact with the API. You can use tools like Postman or cURL to test GET and POST requests.

This is just a basic guide to get you started with adding an API to your Django project. You can expand the API with more views, serializers, authentication, pagination, and filtering as needed.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Praveen Xavier
Praveen Xavier

Written by Praveen Xavier

1's and 0's most effective language ever. Proud Coder, Geek, Writer, GameDev

No responses yet

Write a response