Instead of datetime.datetime.now()
use timezone.now()
:
from django.utils import timezone
timezone.now()
سؤال
I'm trying to query the database and get the entries for today. So I got a model Events with a date time field. Just to clarify, if I remove the date filter, it does return entries from the database. If I add them it does not. I double-checked that there is an item for today.
views.py
def dashboard(request):
if request.user.is_authenticated():
now = datetime.datetime.now()
events_today = Event.objects.filter(date__year=now.year, date__month=now.month, date__day=now.day)
return render_to_response("dashboard.html", {'today': events_today,}, RequestContext(request))
Update
It does work if I change the USE_TZ
to False
in settings.py
. But it doesn't if it's True
.
Update 2
I even uploaded the project to my VPS just in case it had something to do with my computer but still the same.
المحلول
Instead of datetime.datetime.now()
use timezone.now()
:
from django.utils import timezone
timezone.now()
نصائح أخرى
I think you can try this query:
events_today = Event.objects.filter(date=datetime.datetime.today())