Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Introduction to Django REST Framework

1. What is Django REST Framework?

Django REST Framework (DRF) is a powerful and flexible toolkit for building Web APIs. It is built on top of Django and helps in creating RESTful APIs with ease. DRF provides a set of tools and functionalities to simplify the process of building APIs, including serialization, authentication, and viewsets.

2. Setting Up Django REST Framework

To get started with Django REST Framework, you first need to install Django and DRF. You can do this using pip, the Python package installer.

Run the following commands in your terminal to install Django and DRF:

pip install django
pip install djangorestframework

After installing the necessary packages, create a new Django project:

Run the following command:

django-admin startproject myproject

Navigate to the project directory:

Run the following command:

cd myproject

Create a new Django app:

Run the following command:

python manage.py startapp myapp

3. Configuring Django REST Framework

Once the project and app are set up, you need to add Django REST Framework to your project settings. Open the settings.py file in your project directory and add 'rest_framework' to the INSTALLED_APPS list:

INSTALLED_APPS = [
    ...
    'rest_framework',
    'myapp',
]
                

4. Creating a Simple API

Let's create a simple API to manage a list of books. First, define a Book model in the models.py file of your app:

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
                

Next, create a serializer for the Book model in a file named serializers.py:

from rest_framework import serializers
from .models import Book

class BookSerializer(serializers.ModelSerializer):
    class Meta:
        model = Book
        fields = '__all__'
                

5. Creating Views

Now, create views for your API in the views.py file:

from rest_framework import viewsets
from .models import Book
from .serializers import BookSerializer

class BookViewSet(viewsets.ModelViewSet):
    queryset = Book.objects.all()
    serializer_class = BookSerializer
                

6. Configuring URLs

To make your API accessible, you need to configure the URLs. Open the urls.py file in your app directory and add the following:

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)),
]
                

7. Running the Server

Finally, run the Django development server to see your API in action. Run the following command in your terminal:

python manage.py runserver

Open your web browser and navigate to http://127.0.0.1:8000/books/ to see your API.

8. Conclusion

Congratulations! You have successfully created a simple API using Django REST Framework. This tutorial covered the basics of setting up a Django project, installing DRF, and creating a simple API. With DRF, you can build more complex and robust APIs for your applications.