DEV Community

Cover image for RESTful API in Django [Django Rest Framework]
Bek Brace
Bek Brace

Posted on

RESTful API in Django [Django Rest Framework]

Mastering Django REST Framework: A Guide to Building RESTful APIs
By @bekbrace aka. Amir Bekhit

Hey there, Python developers [and other fellow devs].
If you're diving into web development with Django, adding the Django REST Framework (DRF) to your toolkit will be a game-changer.
It makes building RESTful APIs straightforward and efficient.
In this post, I'll introduce you to RESTful APIs and how to use DRF to build a simple yet powerful library management system (I love books!)
Plus, I've included a video where I demonstrate coding examples to create, edit, delete, and view books.

The Video Guide

Check out my video, where I demonstrate these steps in more detail. You'll see how to create a full CRUD API to manage books, complete with authentication and a browsable UI for testing - if you don't want to watch, continue reading then :)

What is a RESTful API?

RESTful APIs (Representational State Transfer) adhere to architectural principles that ensure flexibility and scalability. The primary idea is to access resources via standard HTTP methods like GET, POST, PUT, and DELETE. Each resource is accessed via a unique URL endpoint.

What is Django REST Framework?

Django REST Framework is a toolkit that seamlessly integrates with Django to build web APIs. Its key features include:

Serialization: Convert data between Python objects and JSON.
Authentication & Permissions: Support for custom and built-in authentication.
Class-Based Views: Reusable views with method-based HTTP request handling.
Browsable API: An interactive UI for testing APIs directly in the browser.

Getting Started with DRF

Install DRF:

Install Django REST Framework with pip.

pip install djangorestframework
Enter fullscreen mode Exit fullscreen mode

Add to Installed Apps:

Update your settings.py file to include 'rest_framework' in the INSTALLED_APPS section.

INSTALLED_APPS = [
    # other apps
    'rest_framework',
]
Enter fullscreen mode Exit fullscreen mode

Build Your Library Management API

Models: Start by defining the book model.

# models.py
from django.db import models

class Book(models.Model):
    title = models.CharField(max_length=100)
    author = models.CharField(max_length=100)
    published_date = models.DateField()
    isbn = models.CharField(max_length=13, unique=True)
Serializers: Create a serializer to convert the book model to and from JSON.
Enter fullscreen mode Exit fullscreen mode
# serializers.py
from rest_framework import serializers
from .models import Book

class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = '__all__'
Views: Implement views using DRFs class-based views.
Enter fullscreen mode Exit fullscreen mode
# views.py
from rest_framework import viewsets
from .models import Book
from .serializers import BookSerializer

class BookViewSet(viewsets.ModelViewSet):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
URLs: Finally, register the viewsets in your URL configuration.
Enter fullscreen mode Exit fullscreen mode
# urls.py
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import BookViewSet

router = DefaultRouter()
router.register(r'books', BookViewSet)

urlpatterns = [
    path('api/', include(router.urls)),
]
Enter fullscreen mode Exit fullscreen mode

That is it guys, check out the results using CURL [if you're on mac or linux, or you can download git bash on windows] => Check the video for better demonstration.

Conclusion
With the Django REST Framework, building APIs is a awesome (as i live always to say :) ).
I hope this guide gives you a strong foundation to get started. Got questions? Drop them in the comments!

𝕏: https:www.twitter.com/bekbrace

IG: https://www.instagram.com/bek_brace

Top comments (2)

Collapse
 
akshbswas98 profile image
Akash Biswas - 🍻

Adding django rest framework to my backend arsenal.

Collapse
 
bekbrace profile image
Bek Brace

Awesome !