gpt4 book ai didi

python - 如何检查 django 中的生日是否是明天?

转载 作者:行者123 更新时间:2023-12-02 00:03:42 24 4
gpt4 key购买 nike

我正在用 Python 检查学生的生日是今天还是明天。在每个循环中,即使我将 DOB 列更改为今天,生日也不是今天。我该如何解决这个问题?下面是代码。

View .py

def Birthday_alert():
students= Students.objects.all()
tmr= dt.date.today()+ dt.timedelta(days=1)
tmr_bdays=[]
to_bays=[]
# verifying if date of today is working or not
for m in students:
if m.dob == dt.date.today():
print(m.name)

# putting into dictionary
for s in students:
if s.dob == dt.date.today():
to_bays.append({
'name': 's.name',
'course': 's.course.class_name',
'dob' : 's.dob',
'contact': 's.parent_contact',
})
elif s.dob + dt.timedelta(days=1) == tmr:
tmr_bdays.append({
'name': 's.name',
'course': 's.course.class_name',
'dob' : 's.dob',
'contact': 's.parent_contact',
})
else:
for m in students:
print("else:",m.name)
return (tmr_bdays,to_bays)

模型.py

class Students(models.Model):
created_by = models.ForeignKey(
User, on_delete=models.SET_NULL, default=1, null=True)
name = models.CharField(max_length=200, null=True)
dob = models.DateField(null=True, verbose_name='Date of Birth')
age = models.IntegerField()
grade_choice = (
('G1', 'Grade-1'),
('G2', 'Grade-2'),
('G3', 'Grade-3'),
('G4', 'Grade-4'),
('G5', 'Grade-5'),
('G6', 'Grade-6'),
('G7', 'Grade-7'),
('G8', 'Grade-8'),
('G9', 'Grade-9'),
('G10', 'Grade-10'),
('G11', 'Grade-11'),
('G12', 'Grade-12'),
)
gender_choice=(
('M', 'Male'),
('F', 'Female'),
('N', 'None'),
)

blood_choice=(
('O', 'O'),
('A-', 'A-'),
('B+', 'B+'),
('B-', 'B-'),
('A+', 'A+'),
('AB', 'AB'),
)

relation_choice=(
('Uncle', 'Uncle'),
('Aunty', 'Aunty'),
('Father', 'Father'),
('Mother', 'Mother'),
('Grandpa', 'Grandpa'),
('Grandma', 'Grandma'),
('Sister', 'Sister'),
('Brother', 'Brother'),
)
gender=models.CharField(choices=gender_choice, max_length=10, null=True)
grade = models.CharField(choices=grade_choice, max_length=10, null=True)
attending_school = models.CharField(max_length=100, null=True)
course = models.ForeignKey(
Create_Class, on_delete=models.SET_NULL, default=1, null=True)
address = models.TextField(null=True)
parent_name = models.CharField(max_length=200, null=True)
parent_contact = models.CharField(max_length=20, null=True)
parent_address = models.TextField(null=True)
emerg_name = models.CharField(max_length=200, null=True, verbose_name='Emergency Name')
emerg_contact = models.CharField(max_length=20, null=True, verbose_name='Emergency Contact')
emerg_relation = models.CharField(choices=relation_choice ,max_length=20, null=True, verbose_name='Emergency Relationship')
doc_name = models.CharField(max_length=200, null=False, default='None', verbose_name='Doctor Name')
doc_contact = models.CharField(max_length=20, null=False, default='None', verbose_name='Doctor Contact')
blood_type = models.CharField(choices=blood_choice,max_length=10, null=True)
allergic = models.TextField(null=True)

我的期望是知道今天和明天有哪些学生生日!您的帮助将不胜感激。谢谢!

最佳答案

使用daymonth过滤为,

from datetime import timedelta
from django.utils import timezone

datetime_now = timezone.now()
now_day, now_month = datetime_now.day, datetime_now.month
datetime_tomorrow = datetime_now + timedelta(days=1)
tomorrow_day, tomorrow_month = datetime_tomorrow.day, datetime_tomorrow.month

birth_day_today = Students.objects.filter(<b>dob__day=now_day, dob__month=now_month</b>)
birth_day_tomorrow = Students.objects.filter(<b>dob__day=tomorrow_day, dob__month=tomorrow_month</b>)

关于python - 如何检查 django 中的生日是否是明天?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61338711/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com