I cannot create a pre-filled form in python django

I am doing a simple project to practice CRUD in Django and I am working on a simple “To Do” project and these area my codes-

models.py

from django.db import models

# Create your models here.


class ToDoList(models.Model):

    title = models.CharField(max_length = 100, null = False , blank = False)
    description = models.CharField(max_length = 1000 , null = True , blank = True )
    date = models.DateField(null = True , blank = True )

    def __str__(self):
        return self.title 
    

forms.py

from django.forms import ModelForm 
from django import forms 
from .models import ToDoList

class ToDoForm(ModelForm):
    class Meta : 
        model = ToDoList
        fields = ['title','description','date']

        widgets = {
            'title': forms.TextInput(attrs={'class':'form-control'}),
            'description':forms.Textarea(attrs={'class':'form-control'}),
            'date': forms.DateInput(attrs={'type':'date'}),
        }

urls.py

from django.urls import path
from . import views 

urlpatterns = [
    path("",views.homepage,name = 'home_page'),
    path("add-new",views.add_todo,name = 'add_new'),
    path("delete-todo/<todo_id>",views.delete_todo,name = "delete_todo"),
    path("update_todo/<todo_id>",views.update_todo,name = "update_todo"),

    
    
]

views.py

from django.shortcuts import render , redirect 
from .forms import ToDoForm
from django.contrib import messages
from .models import ToDoList




# Codes for the homepage

def homepage(request):
    todos = ToDoList.objects.all()

    return render(request, 'index.html' , {'todos':todos})

# Codes for the add new todo

def add_todo(request):
    form = ToDoForm()

    if request.method == "POST":
        form = ToDoForm(request.POST)
        if form.is_valid():
            form.save()
            messages.success(request,("Added new todo"))
            return redirect('home_page')
        else:
            print('not valid')
            return render(request,'addnew.html', {'form':form})
    
    return render(request,"addnew.html", {"form":form})

# Codes for the update todo (not working properly)

def update_todo(request, todo_id):
    todos = ToDoList.objects.get(pk=todo_id)
    
    if request.method == "POST":
        form = ToDoForm(request.POST, instance = todos)

        if form.is_valid():

            form.save()
            return redirect("home_page")
    
    else:

        form = ToDoForm(instance=todos)


    return render(request , "updatetodo.html", {"form":form, "todos":todos})

# code for deleting todo

def delete_todo(request, todo_id):
    todos = ToDoList.objects.get(pk=todo_id)
    
    if request.method == "POST":

        todos.delete()
        return redirect('home_page')
    else:

        return redirect('home_page')


all the functions work fine except Update Todo. I followed all the steps on to create a pre filled form and its not showing me anything just “The fields are empty oh also this is the html file

updatetodo.html

{% extends "base.html" %}




{% block content %}

    {{todos.title}}


    <form action = "" method = "POST">
        {% csrf_token %}

        {{form.as_p}}

        <div class = "text-center">
           <button class = "btn btn-primary">Submit</button> 
        </div>
    
    </form>

{% endblock %}

please help me find the solution for it.

I used google, django – documentation and followed the steps of this random ytber and still it didn’t work.
Maybe im just bad at understanding english who knows.

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật