【发布时间】:2020-02-12 12:08:43
【问题描述】:
我有一个包含日期和 class_id 的表格。 当用户发布数据时,我想使用日期来查询我的模型以查找具有相同日期的任何实例。但我最终得到了这个错误代码
ValidationError: ["'02/12/2020' value has an invalid date format. It must be in YYYY-MM-DD format."]
这是我的views.py:
if request.method == 'POST':
class_name = get_object_or_404(ClassRoom, pk=request.POST['class_name']) # class name
attendance_date = request.POST['date_field'] # date
# get the students in the class which is current active
# check if the student is current active
students_attendance = StudentAttendance.objects.filter(attendance_date=attendance_date)
我的 models.py 实现是:
class StudentAttendance(models.Model):
student = models.ForeignKey(Student, on_delete=models.CASCADE, related_name='student_attendance')
attendance_date = models.DateField()
status = models.ForeignKey(AttendanceStatus, on_delete=models.PROTECT)
notes = models.CharField(max_length=150, blank=True)
private_notes = models.CharField(max_length=500, blank=True)
#signed_by = models.ForeignKey(Teacher, on_delete=models.CASCADE)
date = models.DateTimeField(auto_now_add=True)
class Meta:
unique_together = (("student", "attendance_date", 'status'),)
ordering = ('-attendance_date', 'student',)
关于如何解决此错误的任何建议...
注意: 1. 我更改了我的 settings.py 以像这样本地化时间
from django.conf.locale.en import formats as en_formart
en_formart.DATE_FORMAT = ('%d %m %y', )
en_formart.DATE_INPUT_FORMATS = ('%m/%d/%Y', '%Y-%m-%d', '%m/%d/%y', '%b %d %Y',
'%b %d, %Y', '%d %b %Y', '%d %b, %Y', '%B %d %Y',
'%B %d, %Y', '%d %B %Y', '%d %B, %Y','%b. %d, %Y')
DATE_FORMAT = 'd/m/ Y'
LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'GMT'
#USE_I18N = True
#USE_L10N = False
没有任何效果我仍然得到同样的错误...请帮助兄弟
【问题讨论】:
-
格式化系统默认是关闭的。要启用它,需要在设置文件中设置
USE_L10N = True。 -
但更新设置后我仍然收到相同的错误消息。py
标签: django python-3.x django-models django-templates django-views