How to Build a CRUD Web App Using Python and Django
In the world of web development, CRUD—Create, Read, Update, and Delete—operations are the foundation of most applications. Whether it’s a blog, task manager, or e-commerce platform, CRUD functionality is essential. With Python and the Django framework, building a CRUD web application is both efficient and scalable. Django is known for its simplicity, rapid development capabilities, and built-in features like ORM (Object-Relational Mapping), authentication, and admin interface.
This blog post will guide you through the basic steps to build a CRUD web app using Python and Django.
1. Setting Up Your Django Project
Start by creating a virtual environment and installing Django:
bash
Copy
Edit
python -m venv env
source env/bin/activate # Use `env\Scripts\activate` on Windows
pip install django
Create a new Django project and app:
bash
Copy
Edit
django-admin startproject crud_project
cd crud_project
python manage.py startapp myapp
Register the app in settings.py under INSTALLED_APPS.
2. Define the Data Model
In models.py of your app, define a simple model. For example, a task manager:
python
Copy
Edit
from django.db import models
class Task(models.Model):
title = models.CharField(max_length=100)
description = models.TextField()
created_at = models.DateTimeField(auto_now_add=True)
def __str__(self):
return self.title
Run migrations:
bash
Copy
Edit
python manage.py makemigrations
python manage.py migrate
3. Create Views for CRUD Operations
In views.py, create views for each CRUD function:
Create: Form to add a new task
Read: Display list of tasks
Update: Edit an existing task
Delete: Remove a task
Example (Read view):
python
Copy
Edit
from django.shortcuts import render
from .models import Task
def task_list(request):
tasks = Task.objects.all()
return render(request, 'task_list.html', {'tasks': tasks})
Use Django’s generic class-based views for efficiency (CreateView, UpdateView, etc.).
4. Set Up URLs
In urls.py of your app and main project, map your views:
python
Copy
Edit
from django.urls import path
from . import views
urlpatterns = [
path('', views.task_list, name='task_list'),
path('add/', views.task_create, name='task_create'),
path('edit/<int:id>/', views.task_update, name='task_update'),
path('delete/<int:id>/', views.task_delete, name='task_delete'),
]
5. Create Templates
In a templates directory, create HTML files for each operation:
task_list.html: Show all tasks
task_form.html: For create/update forms
task_confirm_delete.html: Confirm before deleting
Use Django template language to render forms and data dynamically.
6. Add Forms (Optional)
You can create a forms.py file and define Django ModelForms for better form handling:
python
Copy
Edit
from django import forms
from .models import Task
class TaskForm(forms.ModelForm):
class Meta:
model = Task
fields = ['title', 'description']
Conclusion
Building a CRUD web app with Django is a great way to learn full-stack development with Python. Django simplifies many aspects of web development, from handling databases to rendering dynamic HTML pages. Once you’ve mastered CRUD, you can expand your project with user authentication, search filters, REST APIs, and deployment to the cloud.
Django makes full stack development fast, clean, and Pythonic!
Read more
How do I run a Python script from a webpage?
How to Become a Full Stack Python Developer from Scratch
Comments
Post a Comment