Python Django窗口小部件和日期字段
在我的创建模板中,日期字段呈现为字符字段,不允许选择日期,而我需要此验证日期 我的模型包含一个日期字段:Python Django窗口小部件和日期字段,python,django,Python,Django,在我的创建模板中,日期字段呈现为字符字段,不允许选择日期,而我需要此验证日期 我的模型包含一个日期字段: class Interview(models.Model): date_planned = models.DateField(verbose_name="Planned Date", blank=True, null=True) 我的视图非常正常地调用模型表单: class InterviewCreateView(UserAccessMixin, Create
class Interview(models.Model):
date_planned = models.DateField(verbose_name="Planned Date", blank=True, null=True)
我的视图非常正常地调用模型表单:
class InterviewCreateView(UserAccessMixin, CreateView):
permission_required = "interviews.add_interview"
model = Interview
form_class = InterviewForm
fields = ['date_planned', 'topics']
template_name = "interviews/interview_create_form.html"
success_url = reverse_lazy("interviews:interviews_list")
```
My Form just calls the Meta and includes a widget:
课堂访谈表(模型表):
definit(self,*args,**kwargs):
super().init(*args,**kwargs)
self.fields['date_planned'].widget.attrs.update({'size':40,'type':'date'})
事实上,输入字段的大小会增加到40,但是系统会忽略日期格式,并允许我的日期计划字段在forms.py中显示为
CharField
,您可以创建一个类
class DateInput(forms.DateInput):
input_type = 'date'
class Meta:
model = Interview
fields = ['topics', 'date_planned']
widgets = {'date_planned': DateInput(),}
这应该行得通,除非是日期选择器问题。谢谢Dosbodoke完整的答案如下所示:
class DateInput(forms.DateInput):
input_type = "date"
class InterviewForm(ModelForm):
class Meta:
model = Interview
fields = ['topics', 'date_planned']
widgets = {'date_planned': DateInput(),}
非常感谢。你的直截了当的回答提供了一个即时且可操作的解决方案。你的问题是什么?我的问题是,在处理表单时,DateInput被忽略了,在模板中,我只有一个来自“text”而不是“date”的字段。多斯博多克的回答很有效。我很高兴你得到了你需要的帮助。为了将来的参考,你应该问一个直接的问题来获得你需要的帮助。你的评论“我的问题是…”仍然没有提出任何问题。谢谢你的提示!
class DateInput(forms.DateInput):
input_type = "date"
class InterviewForm(ModelForm):
class Meta:
model = Interview
fields = ['topics', 'date_planned']
widgets = {'date_planned': DateInput(),}