问题在于,即使我更改了值并保存了表单,当我不得不重新加载表单或再次进入页面时,文本框也不会显示我保存的值,而是今天的日期。
我发现这个question的答案是将useCurrent
更改为false,但这不能解决我的问题。
我正在使用Django 2.1和postgreSQL进行开发,但是我认为在这种情况下,它与问题没有任何关系,因为如果我从输入中删除类datetimepicker
,日期将正确显示。 / p>
我的实际JS:
var endDateMin = new Date();
endDateMin.setDate(endDateMin.getDate() + 1);
$(function () {
$('.datetimepicker').datetimepicker(
{
format: 'DD/MM/YYYY hh:mm',
minDate: endDateMin,
useCurrent: false,
icons: {
time: "fa fa-clock-o",
date: "fa fa-calendar",
up: "fa fa-arrow-up",
down: "fa fa-arrow-down",
previous: "fa fa-arrow-left",
next: "fa fa-arrow-right",
}
}
);
});
我的表单:(字段为end_date)
class UpdateProjectGeneralForm(forms.ModelForm):
end_date = forms.DateTimeField(input_formats=['%d/%m/%Y %H:%M',])
class Meta:
model = Project
fields = ('name', 'short_description', 'category', 'investment', 'description',
'end_date', 'image')
widgets = {
'description': SummernoteWidget(attrs={'summernote': {
'placeholder': 'Describe your project here...'}}),
'image': FileInput(),
}
def __init__(self, *args, **kwargs):
# first call parent's constructor
super(UpdateProjectGeneralForm, self).__init__(*args, **kwargs)
# there's a `fields` property now
self.fields['investment'].required = True
self.fields['name'].widget = forms.TextInput(
attrs={'placeholder': 'enter the project name here...'})
self.fields['short_description'].widget = forms.Textarea(
attrs={'rows': '2',
'maxlength': '135',
'class': 'textarea-limited',
'placeholder': 'enter a short description of your project limited to 135 characters'})
self.fields['end_date'].widget.attrs['class'] = 'datetimepicker'
#if self.fields['end_date']:
# self.fields['end_date'].widget.attrs['placeholder'] = self.fields['end_date']
#else:
self.fields['end_date'].widget.attrs['placeholder'] = datetime.now().strftime('%d/%m/%Y %H:%M')
self.fields['category'].widget.attrs['class'] = 'selectpicker'
self.fields['category'].widget.attrs['data-live-search'] = 'true'
self.fields['category'].widget.attrs['data-size'] = '5'
self.fields['image'].widget.attrs['class'] = 'btn btn-outline-default btn-file btn-round'
# evade all labels and help text to appear when using "as_crispy_tag"
self.helper = FormHelper(self)
self.helper.form_show_labels = False
self.helper._help_text_inline = True
查看:
class ProjectEditGeneralView(LoginRequiredMixin, SuccessMessageMixin, generic.UpdateView):
template_name = 'webplatform/project_edit_general.html'
model = Project
form_class = UpdateProjectGeneralForm
success_message = 'Project successfully updated!'
def get_success_url(self):
return reverse_lazy('project-edit-general', args=(self.object.id,))
# Make the view only available for the users with current fields
def dispatch(self, request, *args, **kwargs):
project = self.get_object()
# here you can make your custom validation for any particular user
if request.user != project.user:
raise PermissionDenied()
return super().dispatch(request, *args, **kwargs)
# Set field as current user
def form_valid(self, form):
form.instance.user = self.request.user
form.instance.history_change_reason = 'Project Updated'
return super(ProjectEditGeneralView, self).form_valid(form)
模型上的字段:
end_date = models.DateTimeField(
null=True,
blank=True,
help_text=_(
'Select the end date of your project.'
),
)
答案 0 :(得分:0)
我使用Bootstrap-DateTimePicker创建了一个示例Django应用程序,以尝试重现您的问题。
在我的forms.py
中,我将end_date
添加到了widgets
字典中。因此,在您的示例中,您的widgets
词典可能类似于:
widgets = {
'description': SummernoteWidget(attrs={'summernote': {
'placeholder': 'Describe your project here...'}}),
'image': FileInput(),
'end_date': forms.DateTimeInput(attrs={'class': 'datetimepicker'})
}
然后我读到datetimepicker使用MomentJS,在MomentJS中,HH
对应24小时,而hh
对应12小时。在Python时间字符串中,您使用的是24小时制,因此,您可能希望datetimepicker的format
使用HH
而不是hh
:
$('.datetimepicker').datetimepicker(
{
...
format: 'DD/MM/YYYY HH:mm',
...
}
);
我还阅读了其他几篇文章,其中用户对日期/时间格式有疑问,因为他们在JS端使用的格式与在Python端使用的格式不同,因此请务必确保这些是相同。使用这种格式,我能够获取datetimepicker以显示正确的日期和时间。
如果您进行了这些更改,是否还会遇到相同的问题?