-1

I want to use the following code to filter the data in models.py in views.py and output the Today_list to today.html, but when I open this url, nothing is displayed. What is the problem?

class Post(models.Model): 
    created =models.DateTimeField(auto_now_add=True,editable=False,blank=False,null=False)
    title =models.CharField(max_length=255,blank=False,null=False)
    body =models.TextField(blank=True,null=False)
    def __str__(self):
        return self.title

views.py

from Todolist import models
from django.views.generic import ListView
from django.utils import timezone

class TodayView(ListView):
    model = models.Post
    template_name ='Todolist/today.html'
    def get_queryset(self):
        Today_list= models.Post.objects.filter(
            created=timezone.now()).order_by('-id')
        return Today_list

todaylist.html

{% extends "Todolist/base.html" %}
{% block content %}
{% for item in Today_list %}
<tr>
    <td>{{item.title}}</td>
</tr>
{% endfor %}
{% endblock %}

urls.py

urlpatterns=[
    path('today/' ,views.TodayView.as_view() ,name='today')
]
Amrez
  • 585
  • 1
  • 5
  • 20
  • Maybe there is no data in the return of `get_queryset` method of `TodayView` class to show it, so nothing is displayed. For checking correctness of this method you can return all of the objects of Post model in `get_queryset` model. – Amrez Nov 02 '22 at 05:48
  • Like you said, I tried importing all the data into the Today_list and it all showed up, but when I did the filter, it stopped showing up. – itsuki fois_ Nov 02 '22 at 09:12
  • 1
    `timezone.now()` gives a datetime object, a datetime object will not be equal to another if they are even a microsecond different... Given you want to filter with a **date** use the [`date` lookup](https://docs.djangoproject.com/en/4.1/ref/models/querysets/#date) – Abdul Aziz Barkat Nov 02 '22 at 14:26
  • Does this answer your question? [How can I filter a date of a DateTimeField in Django?](https://stackoverflow.com/questions/1317714/how-can-i-filter-a-date-of-a-datetimefield-in-django) – Abdul Aziz Barkat Nov 02 '22 at 14:28

2 Answers2

0

use get_context_data to add Today_list to context ref : https://docs.djangoproject.com/en/4.1/ref/class-based-views/generic-display/

def get_context_data(self, **kwargs):
    context = super().get_context_data(**kwargs)
    context['Today_list'] = self.get_queryset()
    return context

or you can simply use in template model.name_list in your case it will be post_list instead of today_list

{% extends "Todolist/base.html" %}
{% block content %}
{% for item in post_list %}
<tr>
    <td>{{item.title}}</td>
</tr>
{% endfor %}
{% endblock %}
0

problem is where you want objects that created today, but your queryset filter just right now, not today. So to achieve today's posts, you can do this:

from django.utils import timezone

def get_queryset(self):
    Today_list = models.Post.objects.filter(
        created__gte=timezone.now().replace(hour=0, minute=0, second=0),
        created__lte=timezone.now().replace(hour=23, minute=59, second=59)
    ).order_by('-id')
    return Today_list

this queryset returns objects that have been created today.

Amrez
  • 585
  • 1
  • 5
  • 20