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.
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 settings.py file and also register installed libraries. Below image will show where to register this install libraries and django app
# open settings.py 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. INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'corsheaders', 'rest_framework', 'api' ]
Create Model for movie collection todo list django app
# models.py 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 serializer.py file in django app " api " directory and write crud operation on this serializer.py file
# serializer.py 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 views.py file in api directory like this or below code is used for viewing crud api
# views.py 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 urls.py file in django project directory
# urls.py 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('admin/', admin.site.urls), path('', include(router.urls)), ]
Register model in admin.py
from django.contrib import admin from .models import Movie admin.site.register(Movie)
Now migrate all app using command and succcessfully migrated run app using runserver
py manage.py makemigrations py manage.py migrate py manage.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.
Popular Post
Leave a reply

Sign in to stay updated on your professional world.
Pro Code Programming
Share and learn to code!
Programming articles, which helps you to build your application.
If something you are stuck to code and complete the program, just find here your quetion related articles.
New to Pro Code Programming?