你好Djangonauts, 我是Django的新手。请原谅代码或逻辑中的任何愚蠢错误
我的表单没有显示他们应该的字段(参见图片)。
price
字段只允许我输入数字,但对数字的数量没有限制甚至可以让我添加15位数字
date
和time_from
,time_to
字段只显示长文本输入字段。我做错了什么?
class LessonsForm(forms.ModelForm):
class Meta:
model = Lessons
fields = ('price', 'quantity', 'date', 'time_from', 'time_to')
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['quantity'].label = "How many members in one class"
self.fields['price'].label = "How much do members have to pay to take lessons from you"
self.fields['date'].label = "When do you plan to offer lessons"
self.fields['time_from'].label = "What time do the lessons start"
self.fields['time_to'].label = "What time does the lessons end"
widgets = {
'price': forms.DecimalField(decimal_places=2, max_digits=5),
'quantity': forms.IntegerField(min_value=1, max_value=25),
'date': forms.DateField(format("%b %d %Y")),
'time_from': forms.TimeField(format('%H:%M')),
'time_to': forms.TimeField(format('%H:%M'))
}
以下是型号
class Lessons(models.Model):
user = models.ForeignKey(User)
post = models.ForeignKey(Post)
price = models.DecimalField(max_digits=5, decimal_places=2)
quantity = models.PositiveIntegerField()
date = models.DateField()
time_from = models.TimeField()
time_to = models.TimeField()
def get_absolute_url(self):
return reverse('posts:single', kwargs={'username': self.user.username,
'slug': self.post.slug})
答案 0 :(得分:1)
我知道我的回复很晚,但是对以后寻找答案的人可能会有帮助。
TimeField被加载为“文本输入”字段,因为在呈现表单时,我们需要明确提及特定字段是“ Time”字段。
在您的问题中,您希望时间以HH:MM AM/PM
格式显示,为此,我们可以将Timeinput小部件用作:
widget=forms.TimeInput(format='%I:%M %p', attrs={'type': 'time'})
在这里,通过attrs
,我们将输入类型明确定义为“时间”输入。