我正在尝试过滤我的前两个IF语句,以仅显示今天当前日期从上午8点到上午10点的当前日期,现在只是日期,我需要将date = date更改为created_at =”,但是将在一定范围内完成。这是我的views.py。如何将此添加到我的IF语句中?谢谢
views.py
def Student_Progress(request, studentpsid):
if request.method == "GET":
date = datetime.date.today()
# Calculates Points on Student Progress Page
academic = K8Points.objects.values_list('academic', flat=True).filter(
student_name_id=studentpsid).filter(date=date)
behavior = K8Points.objects.values_list('behavior', flat=True).filter(
student_name_id=studentpsid).filter(date=date)
my_class_id = request.session['my_class_id']
academic_total = 0
behav_total = 0
for score in academic:
academic_total += int(score)
for score in behavior:
behav_total += int(score)
grand_total = academic_total + behav_total
counseling = Student.objects.values_list(
'counseling_goal', flat=True).get(studentpsid=studentpsid)
studentid = Student.objects.get(studentpsid=studentpsid)
k8obj = K8Points.objects.filter(
student_name_id=studentpsid, date=date).order_by('time_frame')
# Checks To See If a Student Gets Morning Recess
if grand_total >= 20 and K8Points.objects.filter(time_frame=1).filter(date=date) and K8Points.objects.filter(time_frame=2).filter(date=date) and K8Points.objects.filter(time_frame=3).filter(date=date):
morning_recess = "YES"
context = ({'studentid': studentid, 'date': date, 'counseling': counseling, 'grand_total': grand_total, 'academic_total': academic_total,
'behav_total': behav_total, 'k8obj': k8obj, 'my_class_id': my_class_id, 'morning_recess': morning_recess})
return render(request, 'points/student_progress.html', context)
if grand_total <= 20 and K8Points.objects.filter(time_frame=1) and K8Points.objects.filter(time_frame=2) and K8Points.objects.filter(time_frame=3).filter(date=date):
morning_recess = "NO"
context = ({'studentid': studentid, 'date': date, 'counseling': counseling, 'grand_total': grand_total, 'academic_total': academic_total,
'behav_total': behav_total, 'k8obj': k8obj, 'my_class_id': my_class_id, 'morning_recess': morning_recess})
return render(request, 'points/student_progress.html', context)
else:
context = ({'studentid': studentid, 'date': date, 'counseling': counseling, 'grand_total': grand_total, 'academic_total': academic_total,
'behav_total': behav_total, 'k8obj': k8obj, 'my_class_id': my_class_id, })
return render(request, 'points/student_progress.html', context)
models.py
class K8Points(models.Model):
date = models.DateField(default=datetime.date.today())
class_name = models.ForeignKey(TeacherClass, on_delete = models.PROTECT, default = "",)
student_name = models.ForeignKey(Student,on_delete = models.CASCADE, default ="" ,)
week_of = models.IntegerField(default=weeknumber)
day = models.CharField(max_length= 10, default = dayofweek)
TIME_FRAME_CHOICES = [
(None, 'PLEASE SELECT TIME FRAME'), # THIS IS OPTIONAL
(1, '(1.) 8:45AM - 9:00AM'),
(2, '(2.) 9:00AM - 9:30AM'),
(3, '(3.) 9:30AM - 10:00AM'),
(4, '(4.) REC. I 10:00AM -10:10AM'),
(5, '(5.) 10:10AM-10:40AM'),
(6, '(6.) 10:40AM-11:10AM'),
(7, '(7.) 11:10AM-11:40AM'),
(8, '(8.) REC II LUNCH 11:40AM-12:20PM'),
(9, '(9.) 12:20PM-12:50PM'),
(10,'(10.) 12:50PM-1:20PM'),
(11,'(11.) 1:20PM-1:50PM'),
(12,'(12.) 1:50PM-2:20PM'),
(13,'(13.) REC. III 2:20PM-2:30PM'),
]
time_frame = models.PositiveSmallIntegerField(choices=TIME_FRAME_CHOICES,)
behavior = models.IntegerField(default="", validators=[
MaxValueValidator(5),
MinValueValidator(1)
])
academic = models.IntegerField(default="", validators=[
MaxValueValidator(5),
MinValueValidator(0)
] )
total = models.IntegerField(default=0 )
morning_recess= models.CharField(max_length= 3, blank = True ,default = "" )
created_at = models.DateTimeField(auto_now_add=True)
答案 0 :(得分:1)
今天过滤器
today_date = datetime.datetime.now().date()
.filter(your_datetime_field__date = today_date)
小时过滤器
.filter(your_datetime_field__hour__lte = 10).filter(your_datetime_field__hour__gte = 8)
请记住,此查询的结果也将是10:59:59。
如果您只需要8:00:00-9:59:59,请使用lt
代替lte