Home python Django Mysql Rest Api Crud Application

Django Mysql Rest Api Crud Application

8 min read

This Django Crud tutorial will teach you how to do basic database functions that are CREATE RETIEVE, UPDATE and DELETE and SEARCH using mysql Database. The INSERT, SELECT, UPDATE and DELETE statements can be used in any database system, because this is support by all relational database systems.Django is world best Python Framework for developing the webapplications.

Let Learn How to Make the System Step by Step

Install the Django

pip install django==3.2

After install Django lets create the django Project

Create Django Project

django-admin startproject SchoolProject .

Lets Run the Project Using Following Command

python manage.py runserver

Now you can see the welcome page of django.

After that lets configure the database mysql.

Lets Install Mysql

pip install pymysql

After that lets Upgrade the Version

pip install pymysql --upgrade

After that go to mysql database i used in this example xampp server. and create the database kms.

Lets Create the Virtual Environment type the following command

python -m venv Env

after run the command there is a folder created which name is Env.inside the Env folder there is file which is created Scripts. lets go inside the folder type the following command

cd Env\Scripts

then need to activated so type the following command


Now you can see the Virtual Environment is activated successfully. then type the following command. come to backward

cd ../..

now you in the Virtual Environment lets install the Django again

pip install django==3.2

After that will create the App which name is StudentApp

python manage.py startapp StudentApp

StudentApp we have to added in to the setting.py


After that Create the Model


from django.db import models

class Student(models.Model):
    name = models.CharField(max_length = 255)
    address = models.CharField(max_length = 255)
    fee = models.IntegerField()

Install the CORS

pip install django-cors-headers

Install the Rest Framework

pip install djangorestframework

Add the dependencies in to the settings.py inside the INSTALLED_APPS

in to the settings.py inside the MIDDLEWARE add this dependencies

in to the settings.py you have add CORS_ORIGIN Setting make both as true


After that select StudentApp Folder inside the create the file serializers.py


from rest_framework import serializers
from StudentApp.models import Student

class StudentSerializer(serializers.ModelSerializer):
    class Meta:
        model = Student
        fields = '__all__'

After that lets Configure the Mysql Database into our Django Project

 Install the database Url  type by the following command

pip install dj-database-url

After that Install the mysqlclient

pip install mysqlclient

After that Configure the database connection on settings.py

import dj_database_url

DATABASES['default'] = dj_database_url.parse('mysql://root@localhost/kms')

After that run the migration command 

python manage.py makemigrations

After done successfully. then type following command

python manage.py migrate

then you can see you database table has been created.

After that open views.py for generating views

views.py paste the code

from django.views.decorators.csrf import csrf_exempt
from rest_framework.parsers import JSONParser
from django.http.response import JsonResponse
from StudentApp.serializers import StudentSerializer
from StudentApp.models import Student

def studentApi(request,id=0):
    if request.method=='GET':
        student = Student.objects.all()
        return JsonResponse(student_serializer.data,safe=False)
    elif request.method=='POST':
        if student_serializer.is_valid():
            return JsonResponse("Added Successfully",safe=False)
        return JsonResponse("Failed to Add",safe=False)
    elif request.method=='PUT':
        if student_serializer.is_valid():
            return JsonResponse("Updated Successfully",safe=False)
        return JsonResponse("Failed to Update")
    elif request.method=='DELETE':
        return JsonResponse("Deleted Successfully",safe=False)

Manage the Urls

Urls.py paste the code

from django.contrib import admin
from django.urls import path
from django.conf.urls import url
from StudentApp import views

urlpatterns = [
    path('admin/', admin.site.urls),

Run the Project

python manage.py runserver

I have attached the video link below. which will do this tutorials step by step.













Load More Related Articles
Load More By admin
Load More In python

Leave a Reply

Your email address will not be published. Required fields are marked *

Check Also

Build Crud App with Laravel 11 and Vue.js

In this tutorial will teach Vue Laravel 10 CRUD using Vite step by step. Laravel  10 CRUD …