Build a simple To-Do List App with Django

·

4 min read

We can put all of the tasks that we need to complete on a given day by listing it all as a To-Do list. It can be very useful for managing the time as we plan a day ahead of time and prioritize some activities.

In this article, we will be creating a basic To-Do application with Django implementation. After the end of this page, we will be able to add new items to the list and delete the items from the list with the app we are about to create. Even if we don't know much of Python, Django and HTML, we can keep up with this simple project and move forward to creating our next interesting applications.

1/ Install Django: make sure to have Django installed in the system.

$ pip install django

2/ Start a new Project:

Open the command prompt or terminal in the folder that you want to create the project.

$ django-admin startproject todoproject

A new folder called todoproject with some files will be created.

image.png

Go inside the todoproject folder by typing following command

$ cd todoproject

Let's try to run the project using:

$ python manage.py runserver

Default django project will be running on the server

image.png

3/ Create an App by typing a command

$ python manage.py startapp todoapp

Now, open 'settings.py' file and add the app name in INSTALLED_APPS list.

1.png

4/ Create a Template inside todoproject

$ mkdir templates

Inside templates directory, create new HTML file called 'todolist.html' Add a heading :

<h1>My To Do List</h1>

Now, go to views.py file and add a view for the template

from django.shortcuts import render
def todoappView(request):
    return render(request, 'todolist.html')

We need to link this view to the URL. Go to urls.py and make the following changes:

from django.contrib import admin
from django.urls import path
from todoapp.views import todoappView

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

Go to settings.py file and make the changes to the TEMPLATES list: Find the section called DIRS and replace with following. Django will look at this directory for templates

DIRS': [os.path.join(BASE_DIR, 'templates')],

Run server and go to 127.0.0.1:8000/todoapp

image.png

5/ Create a Model. A model that represents each todo item that we create and store in a database.

In the 'models.py' and add the following code:

from django.db import models
class TodoListItem(models.Model):
    content = models.TextField()

This model represents each item on the todo list. We will use this model to interact with database

Now we need to do migrations. It is to update the configuration in database.

$ python manage.py makemigrations

After that:

$ python manage.py migrate

The result will be similar to

image.png

6/ Create the To-Do List

Let's try to retrieve todo items to the view. Open views.py file and import the model

from .models import TodoListItem

Create a new variable to store all the todo items then return this list to the template file. Make the following changes in the view:

def todoappView(request):
    all_todo_items = TodoListItem.objects.all()
    return render(request, 'todolist.html', {'all_items':all_todo_items})

In the above we returned the list 'all_todo_items' as dictionary.

Go to the templates folder and create a list of todo items. Open todolist.html and add the following code:

<ul>
    {% for i in all_items %} 
    <li>{{i.content}}
    </li>
    {% endfor %}
</ul>

We're using looping to iterate through the list of items we have in the all_items variable.

Create a form just below the heading so that users can add the todo items to the list.

<form action="/addTodoItem/" method = "post">{% csrf_token %}
    <input type="text" name="content">
    <input type="submit" value="Add Todo Item">
</form>

The action=”/addTodoItem/” will send an HTTP request to that URL. We’ll be using the POST request here. We need a tag {% csrf_token %} for security purposes.

Now refresh the URL,

In the views.py to create a view to handle the post request.

def addTodoView(request):
    x = request.POST['content']
    new_item = TodoListItem(content = x)
    new_item.save()
    return HttpResponseRedirect('/todoapp/')

We need to import HttpResponseRedirect:

from django.htttp import HttpResponseRedirect

In the urls.py and create a path for the view that we've just created

path('addTodoItem/', addTodoItem),

Also, Don't forget to import the addTodoView to the urls.py file:

from todoapp.views import todoappView, addTodoView

When we refresh the page

image.png

7/ Adding Delete Buttons: Inside the todolist.html file and create delete buttons for each todo list item.

To do that add the in between the list tags just after the content.

<form action="/deleteTodoItem/{{i.id}}/" method = "post">{% csrf_token %}
            <input type="submit" value="Delete">
</form>

We will use the id for identifying each specific item from the list.

In the urls.py add a new path for the delete requests.

path('deleteTodoItem/<int:i>/', deleteTodoView),

Integer i is used as a primary key to identify the specific item in the list that needs to be deleted.

Make sure we import the deleteTodoView in this file.

from todoapp.views import todoappView, addTodoView, deleteTodoView

Inside the views.py and create a new view for the delete requests.

def deleteTodoView(request, i):
    y = TodoListItem.objects.get(id= i)
    y.delete()
    return HttpResponseRedirect('/todoapp/')

The simple To-Do application is completed, let us try to check it by running

image.png

GitHub link