我在我的模型中有一個DateTime
字段,我正在尋找一種簡單的方法使它在表單中看起來不錯。類似SelectDateWidget
。Django DateTimeField輸入形式
我一直在尋找很多類似的問題,看起來非常棘手,讓管理員datepicker或jquery的工作。 (這是我第一次使用Django,並且從未使用過jquery)。
因此,我使用ChoiceField
而不是從this的例子,但我無法讓它工作。我得到錯誤名稱'自我'未定義。我在這裏不能使用self
嗎?還是有一些更好的簡單的方式來做到這一點?我不需要花哨的日期選擇器,只是讓輸入變得容易。
class ProjectForm(ModelForm):
startdate = forms.DateField()
starthour = forms.ChoiceField(choices=((6,"6am"),(7,"7am"),(8,"8am"),(9,"9am"), ...))
startminute = forms.ChoiceField(choices=((0,":00"),(15,":15"),(30,":30"),(45,":45")))
class Meta:
model = Project
def clean(self):
starttime = time(int(self.cleaned_data.get('starthour')),
int(self.cleaned_data.get('startminute')))
return self.cleaned_data
try:
self.instance.start_time = datetime.datetime.combine(
self.cleaned_data.get("startdate"), starttime)
except TypeError:
raise forms.ValidationError("")
Youy'try'塊應該在'clean'功能下。 – Ahsan 2013-03-08 10:43:18