Share and Learn To Write Code!

Programming articles, which help you to build your application. If something you are stuck to code and complete the program, just find here your question-related articles.

Share with   

How to write python django rest api for backend developement - Part 3

We are here to see how we can write web API using Python language and the Django framework. Using this, we can see a demo for the movie collection todo list.
Python   Django   Rest API   Web Services  

Django rest api for backend developement - Part 3

In previous part 1 and part 2 we are successfully created django app for movie collection todo list. If you not seen this parts please check part 1 and part 2 before starting web api if you don't know how to install django and start django server.

click here for part 1

click here for part 2

In this part 3 we are going to create movie collection todo list in django rest framework with CRUD operations

Lets start with short from part 1 and part 2

mkvirtualenv MovieTodoListProject

#If virtual environment not started please use below command to start project virtual environment

workon MovieTodoListProject

pip install django

django-admin startproject MovieToDoList

Open this project in any python editor. I prefer Visual Code

Install all necessary libraries for movie collection todo list api

pip install django-rest-framework django-cors-headers

Now create Django App for start writing crud operation on that app and register it on file and also register installed libraries. Below image will show where to register this install libraries and django app

# open file and do changes on your app like this

CORS_ORIGIN_WHITELIST = ['http://localhost:4200'] # this is your frontend framework server name if you use 3rd party framework.


Create Model for movie collection todo list django app

# file in api direcotry

from django.db import models

class Movie(models.Model): 
    title = models.CharField(max_length=32) 
    content = models.TextField()
    year = models.IntegerField()

Create file in django app " api " directory and write crud operation on this file

# file in api directory

from rest_framework import serializers 
from .models import Movie

class MovieSerializer(serializers.HyperlinkedModelSerializer): 
    class Meta: 
        model = Movie 
        fields = ('id','title','content','year')

Do changes in file in api directory like this or below code is used for viewing crud api

# file in api directory

from rest_framework import viewsets 
from api.serializer import MovieSerializer 
from api.models import Movie

class MovieViewSet(viewsets.ModelViewSet): 
    queryset = Movie.objects.all() 
    serializer_class = MovieSerializer

Do changes in file in django project directory


from django.contrib import admin
from django.urls import path, include
from rest_framework import routers
from api import views

router = routers.DefaultRouter()
router = routers.DefaultRouter() router.register(r'movies', views.MovieViewSet)

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

Register model in

from django.contrib import admin
from .models import Movie

Now migrate all app using command and succcessfully migrated run app using runserver

py makemigrations

py migrate

py runserver

Congrats you have successfully created CRUD api for movie collection todo list.

If you want to see CRUD operation using postman software please read the final part i.e. part 4 by click here.

Leave a reply
Registerd Users
Total Subscribers
Total Articles
Total Views