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.
Hello, we want a full course API rest framework ( backEnd )
( a complete project with the addition of pictures, tokens and everything)
What tut realy like you have explained it beautifully
You have explained this beautifully . Keep up the good work!
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
Thanks!
Nice thank you so much , like 👍 and subscribed
it's so goog
Is it recommended to pass in id in a form-data or as a url parameter like delete-post/id ?
well explained 💯💯✌️
Pls what’s the name of the extension you installed on vs code that was suggesting code
Greate job sir. i learned a lot with this video. thank you very much!!!
my code has an error the method is not allowed, why?
I've been away with Django for almost 3 years, nice refresher thank you brother for putting this up.
Very informative! Thank youu
Very helpful 👏
Lovely bro
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