Building a To Do Application in Python using Django Framework – Part 02

In this second part we are going to create the URLs and the views for our ToDo application. Let’s get started!

Step 1 – Creating URLs to the home app and todo app

In the todoapp folder (which is the project) we have a file named I modify it to look like shown below:

from django.conf.urls import url, include
from django.contrib import admin
import home.views as home_views
from todo import urls as todo_urls

urlpatterns = [
    url(r'^$', home_views.get_index, name="home"),
    url(r'^todo/', include(todo_urls)),

In the urlpatterns section the link to the admin app is created automatically when the project is created.

The second URL, to the home page, says: if after the domain name there is nothing (r'^$', see regular expressions in Python for further explanations), go to the home folder, into views file (home_views is home.views, see line 3: import home.views as home_views), and run the function get_index. This URL we named home. We haven’t created yet the function get_index but we will.

The third URL, to the todo app, links to another file. That’s because the todo app will contain more than one page (edit, delete etc). So it says: if after the domain name is todo/ go to the todo folder and look for there (which is imported here as todo_urls).

Step 2 – Creating the views

In the home folder, open the file and paste the following code into it:

from django.shortcuts import render

def get_index(request):
    return render(request, "home/index.html")

The get_index function accepts one argument, request, which is a must, and all it returns is an HTML page which is named index.html. This page is not created yet but we will create it.

This is it with the views for the home app.

To create the views for the todo app, we have to do an intermediary step. Within the todo folder we have to create an file which will contain the urls to the individual pages such as ‘delete.html’ or ‘edit.html’ because we need to be able to edit or delete a to do item. Let’s create the file and the links.

In the newly created file within the todo folder, paste the following code:

from django.conf.urls import url
from .views import *

urlpatterns = [
    url(r'^$', get_index, name="todo_index"),
    url(r'edit/(\d+)$', edit_todo_item, name="todo_edit"),
    url(r'delete/(\d+)$', delete_todo_item, name="make_todo_go_bye_bye"),
    url(r'toggle/(\d+)$', toggle_status, name="todo_toggle"),

Now, let’s not forget that these urls follows the following url:
The (\d+) means a digit or more. This is the id given automatically by Django to an item when is created. So, if we want to edit an item for example, we need to use its id to know which item to be displayed on the page for editing.

These urls link to a function in a view which in turn will do something. We will see what these functions do when we create them.

Let’s create some simple views that render simple pages.

Within the todo folder, in the file, paste the following code:

from django.shortcuts import render

def get_todo_index(request):
    return render(request, "todo/index.html")
def edit_todo_item(request, id):
    return render(request, "todo/edit.html")
def delete_todo_item(request, id):
    return render(request, "todo/delete.html")
def toggle_todo_item(request, id):
    return render(request, "todo/toggle.html")

Now, we need to create all these templates. Instead of explanations, I am going to show you the folder structure I have. See the picture below:

Step 3 – Registering the apps in the INSTALLED_APPS in

Before going ahead and checking if everything works, we have to register our apps in the file. It should look like shown below:


Now, everything should work just fine. Your urls should link to the views which should render the templates.

That is it for part 2. In part 3 we are going to create the model for our todo item.

Leave a Reply