0

我已将 FloppyForms SplitDateTimeWidget 添加到我的日期时间字段中,如果已经存储了一个值,则时间在表单中按预期正确设置,但日期只是显示为dd/mm/yyyy.

为了让表单正确设置日期值,我缺少什么?

# forms.py
from django import forms

from generic.forms.widgets.floppyforms import SplitDateTimeWidget

from consoles.models import Event


class EventForm(forms.ModelForm):

    class Meta:
        model = Event
        exclude = ('slug',)
        widgets = {
            'qualifying_date': SplitDateTimeWidget(date_format='%d/%m/%Y'),
        }

生成的 HTML 具有正确的 value 属性,但小部件仅显示 'dd/mm/yyyy'

<input type="date" name="start_0" value="21/08/2014" id="id_start_0">
<input type="time" name="start_1" value="10:00:00" id="id_start_1">
4

1 回答 1

0

基于这个问题django: SplitDateTimeWidget ignores date_format,您还必须指定input_date_format

class EventForm(forms.ModelForm):

  class Meta:
    model = Event
    exclude = ('slug',)
    widgets = {
        'qualifying_date': SplitDateTimeWidget(date_format='%d/%m/%Y'),
    }
    qualifying_date = SplitDateTimeField(input_date_formats=['%d/%m/%Y'],
                           input_time_formats=['%H:%M'])
于 2014-08-21T08:00:49.323 回答