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