Breaking News

New Updates

todo app 2023

  1. project --> urls.py
  2. from django.contrib import admin
    from django.urls import path, include
    
    urlpatterns = [
        path('admin/', admin.site.urls),
        path('', include('todoapp.urls'))
    ]
    

  3. app --> urls.py
  4. 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'),
    ]
    

  5. app --> views.py
  6. 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})
    

  7. app --> models.py
  8. 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
    

  9. app --> forms.py
  10. from . models import Task
    from django import forms
    
    # creating easy forms
    class TodoForm(forms.ModelForm):
        class Meta:
            model=Task
            fields = ['name', 'priority', 'date']
    

  11. site --> templates(folder) --> base.html
  12. {% 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>
    

  13. site --> templates(folder) --> home.html
  14. {% 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 %}
    

  15. site --> templates(folder) --> edit.html (forms)
  16. <!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>
    

  17. site --> templates(folder) --> delete.html
  18. {% 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