Creating an API using Django REST Framework: A Comprehensive Guide for 2024

Posted by

Build an API With Django REST Framework | Full Tutorial | 2024

Build an API With Django REST Framework | Full Tutorial | 2024

In this tutorial, we will learn how to build an API using Django REST Framework. Django REST Framework is a powerful and flexible toolkit for building Web APIs in Django. It provides a set of tools and libraries that allow you to easily create RESTful APIs for your Django projects.

Prerequisites

Before we begin, make sure you have the following prerequisites:

  • Python installed on your system
  • Django installed on your system
  • Django REST Framework installed on your system

Step 1: Create a Django Project

First, create a new Django project by running the following command:

django-admin startproject myproject

Step 2: Create a Django App

Next, create a new Django app within your project by running the following command:

python manage.py startapp myapp

Step 3: Setup Django REST Framework

Install Django REST Framework by running the following command:

pip install djangorestframework

Next, add ‘rest_framework’ to your INSTALLED_APPS in settings.py:

INSTALLED_APPS = [
...
'rest_framework',
]

Step 4: Create a Model

Create a model in your app’s models.py file, for example:

from django.db import models

class MyModel(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()

Step 5: Create Serializers

Create serializers in your app’s serializers.py file to convert your model instances to JSON data:

from rest_framework import serializers
from .models import MyModel

class MyModelSerializer(serializers.ModelSerializer):
class Meta:
model = MyModel
fields = '__all__'

Step 6: Create Views

Create views in your app’s views.py file to define how your API endpoints will be accessed:

from rest_framework import viewsets
from .models import MyModel
from .serializers import MyModelSerializer

class MyModelViewSet(viewsets.ModelViewSet):
queryset = MyModel.objects.all()
serializer_class = MyModelSerializer

Step 7: Setup URLs

Finally, setup your app’s URLs in urls.py to map your views to specific endpoints:

from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import MyModelViewSet

router = DefaultRouter()
router.register('mymodel', MyModelViewSet)

urlpatterns = [
path('', include(router.urls)),
]

That’s it! You have now successfully built an API using Django REST Framework. You can now test your API endpoints using tools like Postman or curl.

0 0 votes
Article Rating
17 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
@user-wj5bw5db8m
6 months ago

Hello, we want a full course API rest framework ( backEnd )
( a complete project with the addition of pictures, tokens and everything)

@anasisah2384
6 months ago

What tut realy like you have explained it beautifully

@kidjr.9520
6 months ago

You have explained this beautifully . Keep up the good work!

@mreddygi
6 months ago

if getPost fail on your end, try this:
@api_view(['GET'])
def post(request):
post = get_object_or_404(Post, pk=post_id)
json = PostSerializer(post)
return Response(json.data, status=201)

get_object_or_404 is from django.shortcuts

@javierandres-dev
6 months ago

Thanks!

@mahendranath2504
6 months ago

Nice thank you so much , like 👍 and subscribed

@donaldntsibah530
6 months ago

it's so goog

@andrewglory9434
6 months ago

Is it recommended to pass in id in a form-data or as a url parameter like delete-post/id ?

@Fun_zz
6 months ago

well explained 💯💯✌️

@Dan_Kore
6 months ago

Pls what’s the name of the extension you installed on vs code that was suggesting code

@marcosantonio-pn8kx
6 months ago

Greate job sir. i learned a lot with this video. thank you very much!!!

@mysyaqila3201
6 months ago

my code has an error the method is not allowed, why?

@RealityslapsYT
6 months ago

I've been away with Django for almost 3 years, nice refresher thank you brother for putting this up.

@giovanna010203
6 months ago

Very informative! Thank youu

@tundebadmus7702
6 months ago

Very helpful 👏

@tundebadmus7702
6 months ago

Lovely bro

@harunayakubu3702
6 months ago

Well done sir. I have a question please. Is it compulsory that frontend frameworks like React, Angular and Vue be used when using DRF?
Thanks