To Do List web app

Photo by Emma Matthews Digital Content Production on Unsplash

Let’s start with creating project and app

django-admin startproject ToDoList
django-admin startapp ToDoListApp

settings.py

import os# Build paths inside the project like this: os.path.join(BASE_DIR, ...)BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))DIR = os.path.join(BASE_DIR,'template')STATICDIR = os.path.join(BASE_DIR,'static')#Application definitionINSTALLED_APPS = ['django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','ToDoListApp']#adding static url(e.g css,js,images)STATIC_URL = '/static/'STATICDIRS = [STATICDIR]

models.py

from django.db import models# Create your models here.class NoteSave(models.Model):    Note = models.CharField(max_length=200)    time =models.DateTimeField(auto_now=True, auto_now_add=False)    def __str__(self):        return self.Note

urls.py

from django.contrib import adminfrom django.urls import pathfrom ToDoListApp import viewsurlpatterns = [path('admin/', admin.site.urls),path('', views.index,name='index'),path('submit', views.submit,name='submit'),path('delete/<n>', views.delete,name='delete'),]

forms.py

from .models import NoteSavefrom django import formsclass save(forms.ModelForm):      class Meta:          model = NoteSave           fields = '__all__'

Registering models to Admin

from django.contrib import adminfrom ToDoListApp.models import NoteSave# Register your models here.admin.site.register(NoteSave)

views.py

from django.shortcuts import render,redirectfrom django.urls import reversefrom ToDoListApp.models import NoteSavefrom ToDoListApp.forms import save# Create your views here.def index(req):   notes=NoteSave.objects.all().order_by('time').reverse()   #print(notes)
return render(req,'index.html',{'note':notes})
def submit(req): if req.method=='POST': form1 = save(req.POST) if form1.is_valid: form1.save(commit=True) else: #print('invalid data') return redirect(reverse('index'))def delete(req,n):
#print(n)
note = NoteSave.objects.filter(Note=n) note.delete() return redirect(reverse('index'))

index.html

{% load static %}<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta http-equiv="X-UA-Compatible" content="ie=edge"><link rel="stylesheet" href="{% static 'css/bootstrap.min.css' %}"><title>Document</title></head><body class="bg-dark" ><h1 class="text-white">This is  ToDoList</h1><div class="contianer  " ><div class="container justify-content-md-center col-sm-3 mt-md-5"><div class=alert-success>{{msg}}</div><form action="{% url 'submit'  %}" method="POST">{% csrf_token %}<div class="form-group "><input type="text" class="form-control" id="exampleInputText" name="Note" placeholder=" Add Note here..."><input type="submit" class="btn btn-primary" value="Save" name="save" ></a></div></form>{% for i in note %}<div class="contianer  "><div class="card " ><div class="card-item" style="height: 20%;"><center><p >{{i}}</p ></center><small>{{i.time}}</small><small><a href="{% url 'delete' n=i %}">delete</a></small></div></div>{% endfor %}</div></div></body></html>
ToDoList

--

--

--

Engineering student who loves coding

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Enabling HA in Kubernetes based deployments

Photo by Saffu on Unsplash

Everything You Need to Know About DSC Lead

3, 2, 1, ig-listings

Starting A Data Quality Checklist

TryHackMe Write-up: Intro to Python

TOP Mining ROI is 651%! The Mining is Coming Up Soon!

Selection Sort in C.( C program to execute selection Sort algorithm)

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
Shashankk

Shashankk

Engineering student who loves coding

More from Medium

eLearnSecurity Web application Penetration Tester (eWPT) Review

Project 8: Web Server — Room Monitoring and Control System

Embedded Systems Project 7: Web Server

How We Built LYST Website Clone in just 7 days?