1
我試圖在窗體中創建一個TimeInput
字段,並注意到the widget isn't showing correctly。但是當我檢查localhost:8000/admin
時,我看到the widget showing up correctly。爲什麼TimeInput的Django窗口小部件不顯示
我的代碼如下。對於models.py
,
class TimeLimit(models.Model):
before = models.TimeField(blank=True, default=time(7, 0)) # 7AM
after = models.TimeField(blank=True, default=time(23, 0)) # 11PM
對於views.py
,
class UpdateTimeLimitView(LoginRequiredMixin, FormView):
model = TimeLimit
template_name = 'accounts/update_time_limit.html'
form_class = UpdateTimeLimitForm
def get_success_url(self):
return reverse_lazy('accounts:user_profile') + '?username=' + self.request.GET['username']
def get_context_data(self, **kwargs):
data = super(UpdateTimeLimitView, self).get_context_data(**kwargs)
data['username'] = self.request.GET['username']
return data
對於forms.py
,
class UpdateTimeLimitForm(forms.Form):
time_error = {'required': 'This field is required.',
'invalid': 'Please enter valid Hour:Minute values.'}
before = forms.TimeField(widget=forms.TimeInput(format='%H:%M'))
after = forms.TimeField(widget=TimeInput(format='%H:%M'))
class Meta:
model = TimeLimit
最後,在update_time_limit.html
領域相關的部分,
<div class="container">
<form method="post">
{% csrf_token %}
<p>
{% for field in form %}
{{ field.errors }}
<label for="{{ field.id_for_label }}">{{ field.label }}({{ field.help_text }}):</label>
<br />
{{ field }}<br /><br /> and
{% endfor %}
</p>
<input class="btn btn-primary done-btn" type="submit" value="Update Time Limit">
</form>
</div>
有什麼我失蹤或做錯了?謝謝。
謝謝!經過搜索後,我也在這裏看到了與你的StackOverflow類似的答案[http://stackoverflow.com/questions/1450463/django-datetimewidget-not-showing-up]。如果其他人發現它很有用,我會在這裏留下引用:「Django DateInput小部件不會呈現任何內容,只是純文本輸入。但它確實會根據各種格式驗證用戶的輸入,因此它不是無用的。 大多數人會認爲它的行爲與Django Admin界面中的日期選擇器的行爲方式相同,所以它肯定有點令人困惑。「 – user1330974