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

标签 python django python-3.x django-views

我正在用 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/

相关文章:

python - 创建在 python virtualenv 中执行命令的 docker 镜像

python - numpy 用轴上连续元素的总和有效地替换 2d bool 数组

python - Django模板过滤器、标签、simple_tags和inclusion_tags

regex - 为什么我的 Django URL 正则表达式不起作用

python - 引用Django数据模型中的两个外键

python - 如何更改字典中的变量

python - 以与另一个列表相同的顺序放置一个列表

python - Pandas 基于其他两个具有日期时间值的列创建一个 bool 列

python-3.x - 带有打印()的 Python 3 无效语法

python - 如何将答案存储到 numpy 数组中并使用它来创建 Excel 工作表?