Python | ToDo webapp using Django

Prerequisite : django installation

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 : install django
  • crispy_forms :
    pip install --upgrade django-crispy-forms

basic setup :

Start a project by the following command –

django-admin startproject todo-site

Change directory to todo-site –

cd todo-site

Start the server- Start the server by typing following command in terminal –

python manage.py runserver


To check whether the server is running or not go to a web browser and enter http://127.0.0.1:8000/ as URL.

Now stop the server by pressing

ctrl-c

Let’s create an app now.


python manage.py startapp todo

Goto todo/ folder by doing : cd todo and create a folder with index.html file : templates/todo/index.html

Open the project folder using a text editor. The directory structure should look like this :

Now add todo app and crispty_form in your todo_site in settings.py.

Edit urls.py file in todo_site :

filter_none

edit
close

play_arrow

link
brightness_4
code

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 ############
    path('del/', views.remove, name="del"),
    ########################################################################
    path('admin/', admin.site.urls),
]

chevron_right


Edit models.py in todo :

filter_none

edit
close

play_arrow

link
brightness_4
code

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</pre>

chevron_right


Edit views.py in todo :

filter_none

edit
close

play_arrow

link
brightness_4
code

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 recive todo item id from url ##
def remove(request, item_id):
    item = Todo.objects.get(id=item_id)
    item.delete()
    messages.info(request, "item removed !!!")
    return redirect('todo')

chevron_right


Now create a forms.py in todo :


filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Register models to admin :

Navigate to templates/todo/index.html and edit it : link to index.html file

Make migrations and migrate it

python manage.py makemigrations
python manage.py migrate

Now you can run the server to see your todo app

python manage.py runserver



My Personal Notes arrow_drop_up

A 3rd-year Computer Science and Engineering undergraduate student at IERT, Allahabad with an interest in Programming, Data Science/AI and web development

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

3


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.