Make a simple Todo List App using Django

Lets try to build a simple todo list app using Django.

This will help you to install Django and creating a project.

Basic Intro of Django

Django is a Python-based free and open-source web framework that follows the model-template-views architectural pattern.

Lets install Django

pip install django
django-admin startproejct <ProjectName>
python manage.py runserver
python manage.py startapp <App_Name>
INSTALLED_APPS = [
'TodoList', # this should be your app name
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
from django.http import HttpResponsedef index(request):
return HttpResponse("Hello World!")
from django.urls import path
from . import views
app_name = "TodoList" # keep any name or app name
urlpatterns=[
path("",views.index,name="index")
]
from django.contrib import admin
from django.urls import include,path
urlpatterns = [
path('admin/', admin.site.urls), # this is default route path
path('Todolist/', include("TodoList.urls")) # this is routing to your app
]
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Todo List</title>
</head>
<body>
<h1>Todo List</h1>
</body>
</html>
def index(request):
return render(request,"Todolist/index.html")
task = ["foo","bar","baz"]
return render(request,"Todolist/index.html",{
"tasks":task # your List name
})
<ul>
<li> task1 </li>
<li> task2 </li>
<li> task3 </li>
<li> task4 </li>
</ul>
<body>
<h1>Hello {{Name}}</h1> # here Name is variable
</body>
<ul>
{% for task in tasks %}
<li>{{task}}</li>
{% endfor %}
</ul>
from django.http import HttpResponseRedirect
from django import forms
from django.urls import reverse
class NewTextForm(forms.Form):
tasks = forms.CharField(label="New Task:") # forms.CharField will create you a new text input
return render(request,"Todolist/index.html",{
"tasks":task,
"form":NewTextForm(),
})
<form method="POST">
{{form}}
<button type="submit" name="button">Submit</button>
</form>
<form method="POST">
{% csrf_token %}
{{form}}
<button type="submit" name="button">Submit</button>
</form>
def index(request):
if request.method == "POST":
form = NewTextForm(request.POST)
if form.is_valid():
tasks = form.cleaned_data["tasks"]
task.append(tasks)
return HttpResponseRedirect(reverse("todolist:index"))
return render(request,"Todolist/index.html",{
"tasks":task,
"form":NewTextForm(),
})
<form action="{% url 'todolist:index' %}" method="POST">
{% csrf_token %}
{{form}}
<button type="submit" name="button">Submit</button>
</form>

Thanks for Reading the Article!

For adding styles to the page Follow me and check for upcoming Story in adding Styles to Django App.

Enjoy Playing with it!

Follow me in Github, Twitter, Linkedin.

--

--

Full Stack Developer [Web | Mobile]

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store