How to Build a Simple CRUD App with Django: A Step-by-Step Tutorial

Django’s “batteries-included” philosophy makes creating a CRUD (Create, Read, Update, Delete) application remarkably straightforward. In this tutorial, you’ll build a basic task manager that allows users to add, view, edit, and delete tasks. No prior Django experience is required, but you should have Python installed.

Start by creating a Django project and a dedicated app. Run django-admin startproject todo_project, then cd todo_project and python manage.py startapp tasks. Register the app in settings.py by adding 'tasks' to INSTALLED_APPS.

Article illustration

1. Define the Model

Open tasks/models.py and create a simple model:

  • title: CharField(max_length=200)
  • completed: BooleanField(default=False)

Run python manage.py makemigrations and migrate to create the database table.

2. Build the Views and URLs

Use Django’s generic class‑based views for clean code. In tasks/views.py:

  • TaskListView – lists all tasks
  • TaskCreateView – form to add a task
  • TaskUpdateView – edit an existing task
  • TaskDeleteView – confirm deletion

Create a urls.py file in the tasks folder and wire these views to URL patterns, then include them in the project’s main urls.py.

3. Create Templates

Inside tasks/templates/tasks/, create four files:

  • task_list.html – displays all tasks with edit/delete links
  • task_form.html – handles both create and update actions
  • task_confirm_delete.html – delete confirmation page

Use Django’s {% extends %} and {{ form.as_p }} to keep templates minimal.

4. Test Your CRUD Operations

Run the development server with python manage.py runserver. Visit /tasks/ to see an empty list. Use the “Add” link to create a task, edit it, and delete it. Every action updates the database instantly.

Conclusion: You’ve built a fully functional CRUD app in minutes. Django’s generic views and ORM handle the heavy lifting, letting you focus on customizing behavior. Extend this by adding authentication, styling with Bootstrap, or filtering tasks.

sarah antaboga
Author: sarah antaboga

Leave a Reply

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