acelerap.com

Building a Simple To-Do List App with Django: A Step-by-Step Guide

Written on

Chapter 1: Introduction

Creating a straightforward to-do application using Django is an excellent approach to grasp the essentials of this powerful Python web framework. This tutorial will provide a detailed, step-by-step guide on how to establish a Django project, develop a to-do app, and implement features that allow for adding, viewing, and deleting tasks. Let’s get started!

Step 1: Set Up Your Development Environment

Begin by ensuring you have Python and pip installed on your system. You can download Python from the official website, python.org.

Next, install Django using pip:

pip install django

Step 2: Create a New Django Project

Initiate a Django project by executing the following command, which will create a new project titled todo_project:

django-admin startproject todo_project

cd todo_project

To verify that everything is functioning correctly, start the development server:

python manage.py runserver

Open your browser and navigate to http://127.0.0.1:8000/. You should see the Django welcome page.

Step 3: Create a Django Application

Create a new application named todo by running:

python manage.py startapp todo

Include this app in your project by editing todo_project/settings.py to add 'todo' to the INSTALLED_APPS list:

INSTALLED_APPS = [

...

'todo',

]

Step 4: Define Your Models

In todo/models.py, define a model for your notes:

from django.db import models

class Note(models.Model):

title = models.CharField(max_length=100)

content = models.TextField()

created_at = models.DateTimeField(auto_now_add=True)

def __str__(self):

return self.title

After defining your model, run the migrations to create the associated database table:

python manage.py makemigrations

python manage.py migrate

Step 5: Develop Views and Templates

Open todo/views.py and implement the views for listing, adding, and deleting notes:

from django.shortcuts import render, redirect

from .models import Note

def index(request):

notes = Note.objects.all()

return render(request, 'todo/index.html', {'notes': notes})

def add_note(request):

if request.method == 'POST':

title = request.POST['title']

content = request.POST['content']

Note.objects.create(title=title, content=content)

return redirect('index')

return render(request, 'todo/add_note.html')

def delete_note(request, note_id):

note = Note.objects.get(id=note_id)

note.delete()

return redirect('index')

Create templates by establishing a directory named todo/templates/todo and include the HTML files index.html and add_note.html.

Step 6: Configure URLs

To define the app’s URLs, create a urls.py file in the todo directory and add the following content:

from django.urls import path

from . import views

urlpatterns = [

path('', views.index, name='index'),

path('add/', views.add_note, name='add_note'),

path('delete/<int:note_id>/', views.delete_note, name='delete_note'),

]

Integrate these URLs into the main project by editing todo_project/urls.py:

from django.contrib import admin

from django.urls import path, include

urlpatterns = [

path('admin/', admin.site.urls),

path('', include('todo.urls')),

]

Step 7: Launch the Server and Test

Start the development server again:

python manage.py runserver

Test your application by visiting http://127.0.0.1:8000/ in your browser. You should now be able to add, view, and delete notes seamlessly.

Congratulations!

You have successfully built a basic to-do application using Django. This tutorial has introduced you to the fundamental concepts of establishing a Django project, creating models, views, and templates, as well as configuring URLs.

For further learning, consider diving into Django’s official documentation and experiment by adding more features, such as user authentication and note editing.

If you have any questions, feel free to reach out to me via message or through the comments section below!

Here’s how you can support me:

  1. Follow me for more content.
  2. Leave a comment if you found this helpful.
  3. Subscribe to receive updates on my posts.
  4. Join my newsletter for more insights.

Until next time, take care!

Rakan.

Chapter 2: Video Tutorials

To enhance your understanding of the concepts covered, here are some video tutorials:

The first video offers a comprehensive guide on building a To-Do List App with user registration and login functionalities.

The second video walks you through the first part of creating a To-Do List App using Django.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Final Fantasy VII Rebirth: A Game Worth the Wait

A reflection on my brief experience with Final Fantasy VII Rebirth and the importance of patience in gaming.

# Strategies to Combat Tech Burnout: Evening Rituals for Programmers

Discover five effective evening habits that can help programmers and data scientists prevent burnout and maintain motivation.

Reflecting on My Past: Essential Lessons for My Younger Self

A humorous reflection on life lessons I wish I could share with my younger self.