与日期字段分别显示日月日的不同输入 - django
Display different input for day month and year separately from datefield - django
在模板中,我想将每个字段(日、月、年)与 AboutForm
分开显示,而不仅仅是一个输入,如下所示:
models.py:
class About(models.Model):
user = models.OneToOneField(User)
dob = models.DateField(null=True, blank=True)
present_address = models.CharField(max_length=150, null=True, blank=True)
permanent_address = models.CharField(max_length=150, null=True, blank=True)
description = models.TextField(null=True, blank=True)
forms.py:
class AboutForm(forms.ModelForm):
class Meta:
model = About
exclude = ('user',)
您可以使用 SelectDateWidget 小部件。
from django.forms import extras
...
class AboutForm(forms.ModelForm):
class Meta:
model = About
exclude = ('user',)
widgets = {
'dob': extras.SelectDateWidget(years=range(1900, 2015))
}
感谢 likeon 指出这是 Django 内置的。
在模板中,我想将每个字段(日、月、年)与 AboutForm
分开显示,而不仅仅是一个输入,如下所示:
models.py:
class About(models.Model):
user = models.OneToOneField(User)
dob = models.DateField(null=True, blank=True)
present_address = models.CharField(max_length=150, null=True, blank=True)
permanent_address = models.CharField(max_length=150, null=True, blank=True)
description = models.TextField(null=True, blank=True)
forms.py:
class AboutForm(forms.ModelForm):
class Meta:
model = About
exclude = ('user',)
您可以使用 SelectDateWidget 小部件。
from django.forms import extras
...
class AboutForm(forms.ModelForm):
class Meta:
model = About
exclude = ('user',)
widgets = {
'dob': extras.SelectDateWidget(years=range(1900, 2015))
}
感谢 likeon 指出这是 Django 内置的。