【发布时间】:2019-06-10 01:31:02
【问题描述】:
我一直在尝试从 tutorial 添加 bootstrap 4 datepicker。添加新记录时效果很好,但问题是在编辑记录时,该值没有出现在字段中。我所做的是:
widgets.py
from django.forms import DateTimeInput, DateInput
class BootstrapDateTimePickerInput(DateTimeInput):
template_name = 'widgets/bootstrap_datetimepicker.html'
def get_context(self, name, value, attrs):
datetimepicker_id = 'datetimepicker_{name}'.format(name=name)
if attrs is None:
attrs = dict()
attrs['data-target'] = '#{id}'.format(id=datetimepicker_id)
attrs['class'] = 'form-control datetimepicker-input'
context = super().get_context(name, value, attrs)
context['widget']['datetimepicker_id'] = datetimepicker_id
return context
forms.py
class ClaimForm(forms.ModelForm):
purchase_date = forms.DateTimeField(input_formats=['%d/%m/%Y %H:%M'], widget=BootstrapDateTimePickerInput())
class Meta(forms.ModelForm):
model = Claim
fields = ['store', 'expense', 'rewards', 'money_receipt', 'purchase_date']
【问题讨论】:
标签: django python-3.x bootstrap-4