todo app 2023
- project --> urls.py
- app --> urls.py
- app --> views.py
- app --> models.py
- app --> forms.py
- site --> templates(folder) --> base.html
- site --> templates(folder) --> home.html
- site --> templates(folder) --> edit.html (forms)
- site --> templates(folder) --> delete.html
from django.contrib import admin from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('todoapp.urls')) ]
from . import views from django.urls import path urlpatterns = [ path('', views.note, name='note'), path('delete/<int:taskid>', views.delete, name='delete'), path('update/<int:id>', views.update, name='update'), ]
from django.shortcuts import render from . models import Task from . forms import TodoForm from django.shortcuts import render, redirect # Create your views here. def note(request): display = Task.objects.all() if request.method=='POST': name=request.POST.get('task', '') priority=request.POST.get('priority', '') date=request.POST.get('date', '') task=Task(name=name, priority=priority, date=date) # the object name of task created task.save() return render(request, 'home.html', {'task': display}) def delete(request, taskid): task=Task.objects.get(id=taskid) if request.method == 'POST': task.delete() return redirect('/') return render(request, 'delete.html') def update(request, id): task=Task.objects.get(id=id) f=TodoForm(request.POST or None, instance=task) if f.is_valid(): f.save() return redirect('/') return render(request, 'edit.html', {'f':f, 'task':task})
from django.db import models # Create your models here. class Task(models.Model): name = models.CharField(max_length=20) priority = models.IntegerField() date = models.DateField() def __str__(self): return self.name
from . models import Task from django import forms # creating easy forms class TodoForm(forms.ModelForm): class Meta: model=Task fields = ['name', 'priority', 'date']
{% load static %} <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .navabar { background-color: black; width : 100%; height : 100px; } </style> </head> <body> <div class="navabar"> <h1 class="name" >Todo App</h1> <h2><a href="#"></a></h2> </div> {% block content %} {% endblock %} <div class="navabar"> <p class="name" >Made with <b class="love"> ♥ </b> in India</p> </div> </body> </html>
{% extends 'base.html' %} {% block content %} <!-- enter the form task --> <div class="main-body"> <form method="post"> {% csrf_token %} <input type="text" name="task" placeholder="task" required> <input type="number" name="priority" placeholder="priority" required> <input type="date" name="date" placeholder="date" required> <button type="submit">ADD</button> </form> <!-- task output --> <div> <ul> {% for i in task %} <li>{{i.name}}<br>{{i.priority}}<br>{{i.date}}</li> <!-- link for delete page or edite page --> <a href="{% url 'update' i.id %}">update</a> <a href="{% url 'delete' i.id %}">delete</a> {% endfor %} </ul> </div> </div> {% endblock %}
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Edit</title> </head> <body> <form method="post"> {% csrf_token %} <p>{{f.as_p}}</p> <input type="submit"> </form> </body> </html>
{% extends 'base.html' %} {% block content %} <form method="POST"> {% csrf_token %} <p class="card-title">Are you sure this task is Done?</p> <button type="submit">Delete</button> {% endblock %}
No comments