DEV Community

Cover image for Master API Development with Django REST Framework – Learn Django REST Framework for Free!
RathanK
RathanK

Posted on

Master API Development with Django REST Framework – Learn Django REST Framework for Free!

What is Django?
Django is a high-level web framework designed to help developers build web applications quickly and efficiently using the Python programming language. It simplifies the creation of robust, secure, and scalable websites by providing built-in tools for handling common tasks like user authentication, form handling, and database management through its ORM (Object-Relational Mapping).

What is Django Rest Framework (DRF)?
Django Rest Framework (DRF) is an extension of Django that simplifies the process of building RESTful APIs. While Django is primarily used to create traditional web applications with HTML templates, DRF is designed to help developers build APIs that handle requests and responses in formats like JSON and XML.

Why Learn DRF?
Seamless Integration with Django: If you're already familiar with Django or working on a Django project, DRF is the natural choice for adding REST API functionality. It integrates smoothly with Django’s ORM, authentication system, and admin panel.

Rich Ecosystem: DRF provides built-in tools like serializers, viewsets, and routers to minimize code duplication and make API development more efficient.

Mature and Stable: DRF has excellent community support, is widely used in production environments, and comes with extensive documentation.

Security and Scalability: With built-in authentication mechanisms and support for scalability, DRF makes it easier to create secure and scalable APIs.

Browsable API: DRF includes a browsable API interface, making it easy to test and explore your API during development.

Simple Example of a Django Rest Framework API
Here’s a quick example to demonstrate how you can build a simple API using DRF:

Install Django and DRF:
pip install django djangorestframework

Create a New Django Project:

django-admin startproject myproject
cd myproject

Enter fullscreen mode Exit fullscreen mode

Create a New App:

python manage.py startapp myapp

Add rest_framework to Installed Apps in settings.py:

INSTALLED_APPS = [
    ...
    'rest_framework',
    'myapp',
]
Enter fullscreen mode Exit fullscreen mode

Create a Simple Model in myapp/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()

    def __str__(self):
        return self.title
Enter fullscreen mode Exit fullscreen mode

Create a Serializer in myapp/serializers.py:

from rest_framework import serializers
from .models import Book


class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = ['id', 'title', 'author', 'published_date']
Enter fullscreen mode Exit fullscreen mode

Create a View in myapp/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

Enter fullscreen mode Exit fullscreen mode

Set Up URLs in myapp/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('', include(router.urls)),
]
Enter fullscreen mode Exit fullscreen mode

Include the App URLs in myproject/urls.py:

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

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

Run the Server:

python manage.py migrate
python manage.py runserver
Enter fullscreen mode Exit fullscreen mode

Now you have a simple API running at http://localhost:8000/api/books/ that allows you to view, create, update, and delete Book records.

Learn DRF for Free
There are many ways to learn DRF, but learning through a beginner-friendly environment is one of the most effective methods. If you’re looking for a free and comprehensive course, we’ve got you covered!

We’re offering an awesome free course on DRF where you can learn the fundamentals, including filtering, pagination, authentication, and much more.

Don’t miss out! Start learning now by visiting: Free DRF Course

https://rathank.com/courses/api-development-with-django-rest-framework/

Top comments (0)