so I'm still creating my to-do list app, and I want to render all the items that have today's date as the deadline so as to remind the user of the pending tasks.
models.py
from django.db import models
from django.contrib.auth.models import User
# Create your models here.
class ToDo(models.Model):
owner = models.ForeignKey(User, on_delete=models.CASCADE)
todo = models.CharField(max_length=50)
description = models.TextField(max_length=200, blank=True)
created = models.DateField(auto_now=True)
end = models.DateField()
start = models.DateField()
completed = models.BooleanField(default=False)
def __str__(self):
return f'{self.owner} - {self.todo}'
views.py
def index(request):
activities = ToDo.objects.all()
today = ToDo.objects.get(end=datetime.date.today)
todo = ToDo.objects.count()
complete = ToDo.objects.filter(completed=True).count()
percent = complete * 100 // todo
if request.method == 'POST':
try:
search = request.POST.get('todo')
activities = ToDo.objects.filter(todo__icontains=search)
except:
search = request.POST.get('end')
activities = ToDo.objects.filter(end=search)
context = {
'activities' : activities,
'percent' : percent,
'today' : today,
}
return render(request, 'home.html', context)
I imported DateTime in my views.py
CodePudding user response:
Give this a try
from django.utils import timezone
def index(request):
today = timezone.localtime(timezone.now())
deadline_today = ToDo.objects.filter(
end__year=today.year,
end__month=today.month,
end__day=today.day
)
context = {
...
'today' : deadline_today ,
}
CodePudding user response:
You are almost there!
today = ToDo.objects.get(end=datetime.date.today)
Is the problem. get() is used to fetch a single row, you want all the matching rows which is done by filter
. you are also sending in the function today
, instead of calling it with today()
.
Do this:
today = ToDo.objects.filter(end=date.today())