Django CRUD Application with MySQL Database

Recently, I worked on a project for a small business in the USA where they needed a web app to manage customer records. The requirement was simple: store customer details in a database, and allow staff to create, read, update, and delete (CRUD) records easily.

I knew Django + MySQL would be the perfect stack for this. Django provides speed, and MySQL offers a reliable, scalable relational database.

In this article, I’ll walk you through how I set up a Django CRUD application with MySQL. I’ll share the full code so you can follow along and build your project.

Step 1 – Set Up the Django Project

First, let’s create a new Django project.

# Install Django and mysqlclient
pip install django mysqlclient

# Create Django project
django-admin startproject customer_manager

cd customer_manager

# Create an app
python manage.py startapp customers

Now, add customers to the INSTALLED_APPS list inside settings.py.

Step 2 – Configure MySQL Database

Open settings.py and replace the default database with MySQL configuration.

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'customer_db',
        'USER': 'root',
        'PASSWORD': 'yourpassword',
        'HOST': 'localhost',
        'PORT': '3306',
    }
}

Make sure you have already created the database in MySQL:

CREATE DATABASE customer_db;

Step 3 – Create the Model

We’ll store basic customer information: name, email, and city.

Inside customers/models.py:

from django.db import models

class Customer(models.Model):
    name = models.CharField(max_length=100)
    email = models.EmailField(unique=True)
    city = models.CharField(max_length=50)

    def __str__(self):
        return self.name

Run migrations:

python manage.py makemigrations
python manage.py migrate

Step 4 – Create Forms

Inside customers/forms.py:

from django import forms
from .models import Customer

class CustomerForm(forms.ModelForm):
    class Meta:
        model = Customer
        fields = ['name', 'email', 'city']

Step 5 – Views for CRUD Operations

Inside customers/views.py:

from django.shortcuts import render, redirect, get_object_or_404
from .models import Customer
from .forms import CustomerForm

# Read (List all customers)
def customer_list(request):
    customers = Customer.objects.all()
    return render(request, 'customers/customer_list.html', {'customers': customers})

# Create
def customer_create(request):
    if request.method == 'POST':
        form = CustomerForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('customer_list')
    else:
        form = CustomerForm()
    return render(request, 'customers/customer_form.html', {'form': form})

# Update
def customer_update(request, pk):
    customer = get_object_or_404(Customer, pk=pk)
    if request.method == 'POST':
        form = CustomerForm(request.POST, instance=customer)
        if form.is_valid():
            form.save()
            return redirect('customer_list')
    else:
        form = CustomerForm(instance=customer)
    return render(request, 'customers/customer_form.html', {'form': form})

# Delete
def customer_delete(request, pk):
    customer = get_object_or_404(Customer, pk=pk)
    if request.method == 'POST':
        customer.delete()
        return redirect('customer_list')
    return render(request, 'customers/customer_confirm_delete.html', {'customer': customer})

Step 6 – URLs

Inside customers/urls.py:

from django.urls import path
from . import views

urlpatterns = [
    path('', views.customer_list, name='customer_list'),
    path('create/', views.customer_create, name='customer_create'),
    path('update/<int:pk>/', views.customer_update, name='customer_update'),
    path('delete/<int:pk>/', views.customer_delete, name='customer_delete'),
]

Inside customer_manager/urls.py:

from django.contrib import admin
from django.urls import path, include

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

Step 7 – Templates

Create a templates/customers/ folder and add these files:

customer_list.html

<h2>Customer List</h2>
<a href="{% url 'customer_create' %}">Add New Customer</a>
<ul>
  {% for customer in customers %}
    <li>
      {{ customer.name }} - {{ customer.email }} - {{ customer.city }}
      <a href="{% url 'customer_update' customer.id %}">Edit</a>
      <a href="{% url 'customer_delete' customer.id %}">Delete</a>
    </li>
  {% endfor %}
</ul>

customer_form.html

<h2>Customer Form</h2>
<form method="post">
  {% csrf_token %}
  {{ form.as_p }}
  <button type="submit">Save</button>
</form>
<a href="{% url 'customer_list' %}">Back</a>

customer_confirm_delete.html

<h2>Are you sure you want to delete {{ customer.name }}?</h2>
<form method="post">
  {% csrf_token %}
  <button type="submit">Yes, Delete</button>
</form>
<a href="{% url 'customer_list' %}">Cancel</a>

Step 8 – Run the Application

Now, run the server:

python manage.py runserver

Open http://127.0.0.1:8000/ in your browser.

I executed the above example code and added the screenshot below.

crud django MySQL

Enter customer details.

crud in Django
django crud builder
crud operations in python Django

You’ll see the customer list page, and from there you can add, edit, and delete records.

Alternative Method – Use Django REST Framework (Optional)

Sometimes, instead of rendering HTML templates, I expose CRUD operations via an API. This is particularly useful if the frontend is built with React, Angular, or a mobile app.

For this, I use Django REST Framework. It allows me to create serializers and viewsets with just a few lines of code. (I’ll cover this in a separate detailed tutorial.)

Conclusion

That’s it! We just built a fully functional Django CRUD application with MySQL.

This setup is practical for real-world projects in the USA, whether you’re managing customer records for a small business, tracking employee details in an HR system, or building an internal inventory tool.

While this tutorial used simple templates, you can easily extend it with Bootstrap for styling or connect it with a REST API for mobile apps.

You may also read other Django articles:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.