ToDo webapp using Django

Last Updated : 24 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Django is a high-level Python Web framework-based web framework that allows rapid development and clean, pragmatic design. today we will create a todo app created to understand the basics of Django. In this web app, one can create notes like Google Keep or Evernote.

Modules required: Django

Basic setup

Step 1: Start a project with the following command

django-admin startproject todo_site

Step 2:  Change the directory to todo_site.

cd todo_site

To check whether the server is running or not go to a 

Let’s create an app now 

Step 3: Create an app with the following command.

python manage.py startapp todo

The directory structure should look like this:
 

 

Step 4: Now add the todo app in your todo_site in settings.py

 

Step 5: Edit urls.py file in todo_site 

Python3




from django.contrib import admin
from django.urls import path
from todo import views
 
urlpatterns = [
    #####################home_page###########################################
    path('', views.index, name="todo"),
    ####################give id no. item_id name or item_id=i.id ############
    # pass item_id as primary key to remove that the todo with given id
    path('del/<str:item_id>', views.remove, name="del"),
    ########################################################################
    path('admin/', admin.site.urls),
]


Step 6: Edit models.py in todo

Python3




from django.db import models
from django.utils import timezone
 
 
class Todo(models.Model):
    title = models.CharField(max_length=100)
    details = models.TextField()
    date = models.DateTimeField(default=timezone.now)
 
    def __str__(self):
        return self.title


Step 7: Edit views.py in todo

Python3




from django.shortcuts import render, redirect
from django.contrib import messages
 
# import todo form and models
 
from .forms import TodoForm
from .models import Todo
 
###############################################
 
 
def index(request):
 
    item_list = Todo.objects.order_by("-date")
    if request.method == "POST":
        form = TodoForm(request.POST)
        if form.is_valid():
            form.save()
            return redirect('todo')
    form = TodoForm()
 
    page = {
        "forms": form,
        "list": item_list,
        "title": "TODO LIST",
    }
    return render(request, 'todo/index.html', page)
 
 
### function to remove item, it receive todo item_id as primary key from url ##
def remove(request, item_id):
    item = Todo.objects.get(id=item_id)
    item.delete()
    messages.info(request, "item removed !!!")
    return redirect('todo')


Step 8: Now create a forms.py in todo 

Python3




from django import forms
from .models import Todo
 
 
class TodoForm(forms.ModelForm):
    class Meta:
        model = Todo
        fields = "__all__"


Step 9: Register models to admin 

 

Step 10: Create templates/todo/index.html 

HTML




<!DOCTYPE html>
<html lang="en" dir="ltr">
 
<head>
 
  <meta charset="utf-8">
  <title>{{title}}</title>
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <!--style-->
  <style>
  .card {
 
   box-shadow: 0 4px 8px 0 rgba(0,0,0,0.5),
               0 6px 20px 0 rgba(0,0,0,0.39);
   background: lightpink;
   margin-bottom : 5%;
   border-radius: 25px;
   padding : 2%;
   overflow: auto;
   resize: both;
   text-overflow: ellipsis;
  }
  .card:hover{
    background: lightblue;
  }
 
  .submit_form{
 
    text-align: center;
    padding: 3%;
    background: pink;
    border-radius: 25px;
    box-shadow: 0 4px 8px 0 rgba(0,0,0,0.4),
                0 6px 20px 0 rgba(0,0,0,0.36);
  }
  </style>
 
</head>
 
<body  class="container-fluid">
 
  {% if messages %}
  {% for message in messages %}
  <div class="alert alert-info">
    <strong>{{message}}</strong>
  </div>
  {% endfor %}
  {% endif %}
 
  <center class="row">
    <h1><i>__TODO LIST__</i></h1>
    <hr />
  </center>
 
  <div class="row">
 
    <div class="col-md-8">
 
      {% for i in list %}
      <div class="card">
        <center><b>{{i.title}}</b></center>
        <hr/>
        {{i.date}}
        <hr/>
        {{i.details}}
        <br />
        <br />
        <form action="/del/{{i.id}}" method="POST" style=" padding-right: 4%; padding-bottom: 3%;">
          {% csrf_token %}
          <button value="remove" type="submit"  class="btn btn-primary" style="float: right;"><span class="glyphicon glyphicon-trash"></span>   remove</button>
        </form>
      </div>
      {% endfor%}
    </div>
    <div class="col-md-1"> </div>
    <div class="col-md-3" >
      <div  class="submit_form">
      <form  method="POST">
        {% csrf_token %}
        {{forms}}
        <center>
        <input type="submit" class="btn btn-default" value="submit" />
      </center>
      </form>
    </div>
  </div>
</div>
</body>
 
</html>


Step 11: Migrations Files to the Database

python manage.py makemigrations
python manage.py migrate

Step 12: Start the server by typing the following command in the terminal 

python manage.py runserver

Output:

Open the web browser and enter http://127.0.0.1:8000/ as the URL.



Previous Article
Next Article

Similar Reads

Twitter Sentiment Analysis WebApp Using Flask
This is a web app made using Python and Flask Framework. It has a registration system and a dashboard. Users can enter keywords to retrieve live Twitter text based on the keyword, and analyze it for customer feelings and sentiments. This data can be visualized in a graph. This project, in particular, mines data using a popular “Tweepy” API. Tweepy
15+ min read
Daily Latest News webapp Using PyWebio in Python
In this article, we are going to create a web app to get gaily News Using PyWebio As we all download an app for receiving daily news but as a python lovers we try to do all these things via a python script. So here is a python script that notifies the daily news. In this script, we will create a web app using pywebio which shows all the top headlin
5 min read
Create a Simple Sentiment Analysis WebApp using Streamlit
In this article, we will see how we can create a simple Sentiment Analysis webApp using with the help of Streamlit Module in Python. Required Modules: For this, we will need Three Modules. StreamlitTextBlobStreamlit_extras (optional). The module Streamlit_extras is optional because it will be used to add some extra features which will not affect th
4 min read
Adding Lottie animation in Streamlit WebApp
In this article, we will see how we can embed Lottie Animation in our Streamlit WebApp using Python. What is Lottie Animation? An animation file format called a Lottie, which is based on JSON, makes it simple to ship animations across all platforms. They are compact files that may be scaled up or down without pixelation and are compatible with all
5 min read
Todo list app using Flask | Python
There are many frameworks that allow building your webpage using Python, like Django, flask, etc. Flask is a web application framework written in Python. Flask is based on WSGI(Web Server Gateway Interface) toolkit and Jinja2 template engine. Its modules and libraries that help the developer to write applications without writing the low-level codes
3 min read
Python | ToDo GUI Application using Tkinter
Prerequisites : Introduction to tkinter Python offers multiple options for developing GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. In this article, we will learn how to create a ToDo GUI application using Tkinter, with a step-by-step guide. To create a tkinter : Importing the module – tkinter
5 min read
How to make a Todo List CLI application using Python ?
In this article, we see how to make a Command Line application todo list in python. Todo list is a basic application in which users can add items. It’s a list of tasks you need to complete or things that you want to do. In to-do, list tasks are organized in order of priority. One of the most important reasons you should use a to-do list is that it
7 min read
Adding Tags Using Django-Taggit in Django Project
Django-Taggit is a Django application which is used to add tags to blogs, articles etc. It makes very easy for us to make adding the tags functionality to our django project.Setting up Django Project Installing django-taggit pip install django-taggitADD it to Main Project's settings.py file C/C++ Code INSTALLED_APPS = [ 'django.contrib.admin', 'dja
2 min read
How to customize Django forms using Django Widget Tweaks ?
Django forms are a great feature to create usable forms with just few lines of code. But Django doesn't easily let us edit the form for good designs. Here, we will see one of the ways to customize Django forms, So they will look according to our wish on our HTML page. Basically we will check the methods to include our own custom css, classes, or id
3 min read
Integrating Django with Reactjs using Django REST Framework
In this article, we will learn the process of communicating between the Django Backend and React js frontend using the Django REST Framework. For the sake of a better understanding of the concept, we will be building a Simple Task Manager and go through the primary concepts for this type of integration between React js and Django. Reactjs in a nuts
15+ min read
Practice Tags :
three90RightbarBannerImg