【发布时间】:2020-03-23 08:10:32
【问题描述】:
我开发了一个国际化英语/法语的 Django 项目
当用户 webbrowser 为 FR 时,日期必须以 dd/mm/yyyy 格式显示,当用户 webbrowser 为 EN 时,日期必须以 yyyy-mm-dd 格式显示
为此,我使用 JS 来测试 webbrowser 用户最喜欢的语言并相应地显示格式
在我更改我的模型以在此日期添加 unique_together 约束之前效果很好
现在,当网页浏览器使用法语时出现错误,我尝试注册日期 (asp_ent_dat)
'date format "20/03/2020" is invalid. Correct format is "yyy-mm-dd".
models.py:
class Entree(models.Model):
asp_ent_cle = models.AutoField(primary_key=True)
asp_ent_loc = models.CharField("Site concerned by the operation", max_length=10, null=True, blank=True)
med_num = models.CharField("Trial batch number", max_length=3, null=True, blank=True,)
asp_ent_dat = models.DateField("Entry date", null=True, blank=True)
asp_ent_pro_pay = models.CharField("Country of treatment origin in case of entry", max_length=10, null=True, blank=True)
asp_ent_pro_sit = models.CharField("Processing source site in case of entry", max_length=10, null=True, blank=True)
opr_nom = models.CharField("Input operator", max_length=10, null=True, blank=True)
opr_dat = models.DateField("Entry date", null=True, blank=True)
log = HistoricalRecords()
class Meta:
db_table = 'pha_asp_ent'
verbose_name_plural = 'Entries'
ordering = ['asp_ent_cle']
unique_together = ['asp_ent_loc','med_num','asp_ent_dat']
JS:
$(function(){
if(window.navigator.language == 'fr-FR' | window.navigator.language == 'fr'){
$("#id_asp_ent_dat").datepicker(
{
dateFormat: 'dd/mm/yy',
}
);
}
else
{
$("#id_asp_ent_dat").datepicker(
{
dateFormat: 'yy-mm-dd',
}
);
});
forms.py:
def clean(self):
cleaned_data = super(EditForm, self).clean()
cle1 = self.data.get('asp_ent_loc')
cle2 = self.data.get('med_num')
# cle3 = self.data.get('asp_ent_dat') ***LINE THAT RAISE ERROR***
cle3 = self.cleaned_data['asp_ent_dat']
【问题讨论】:
标签: javascript python django date