Django DateField表单删除时间

时间:2015-10-05 17:00:21

标签: django django-forms

我正在将Django的DateField表单与日历小部件结合使用,以允许用户在我的一个视图中指定日期范围。日历窗口小部件允许用户指定除了他们想要的日期之外的时间。发布时,时间会显示在表单中,但应用.cleaned_data会从值中删除时间。

我的表单如下:

class DateRangeForm(forms.Form):
  def __init__(self, *args, **kwargs):
    initial_start_date = kwargs.pop('initial_start_date')
    initial_end_date = kwargs.pop('initial_end_date')
    required_val = kwargs.pop('required')
    input_formats = kwargs.pop('input_formats')

    super(DateRangeForm,self).__init__(*args,**kwargs)
    self.fields['start_date'].initial = initial_start_date
    self.fields['start_date'].required = required_val
    self.fields['start_date'].input_formats = input_formats
    self.fields['end_date'].initial = initial_end_date
    self.fields['end_date'].required = required_val
    self.fields['end_date'].input_formats = input_formats

  start_date = forms.DateField(widget=forms.DateInput())
  end_date = forms.DateField(widget=forms.DateInput())

视图如下所示:

def home(request):
  context_dict = {'boldmessage': "--> To view logs, please turn off any popup blockers <--",'version':"1.0"}

  init_start = convertToDate("2015/2")
  init_end = convertToDate("2015/209")

  if request.method == "POST":
    daterange_form = DateRangeForm(request.POST,required=True,initial_start_date=init_start,initial_end_date=init_end,input_formats=dateFormats)
    if daterange_form.is_valid():
        start = daterange_form.cleaned_data['start_date']
        end = daterange_form.cleaned_data['end_date']
    cont_dict = {'start_date':start,'end_date':end}

    return dummy(request,cont_dict)

  else:
    daterange_form = DateRangeForm(required=True,initial_start_date=init_start,initial_end_date=init_end,input_formats=dateFormats)
    context_dict.update({'daterange_form':daterange_form})

  return render(request, 'InterfaceApp/home.html', context_dict)

我给表单的日期格式如下:

django_dateformats = [ "%Y/%m/%d_%H:%M:%S.%f",  "%Y-%m-%d %H:%M:%S.%f", "%Y-%z-%H:%M",              "%Y-%z-%H:%M",   \
                    "%H:%M:%S",             "%H:%M:%S.%f",                                                          \
                    "%Y-%m-%d",             "%Y/%m/%d",             "%Y/%z",                    "%Y-%z",            \
                    "%Y-%z %H:%M:%S",       "%Y-%z %H:%M:%S.%f",                                                    \
                    "%Y/%m/%d %H:%M:%S.%f", "%Y/%m/%d_%H:%M:%S.%f", "%Y-%m-%d_%H:%M:%S.%f",                         \
                    "%Y/%m/%d %H:%M:%S",    "%Y/%m/%d_%H:%M:%S" ,   "%Y-%m-%d_%H:%M:%S",        "%Y-%m-%d %H:%M:%S",\
                    "%m/%d/%Y %H:%M:%S.%f", "%m/%d/%Y_%H:%M:%S.%f", "%m-%d-%Y_%H:%M:%S.%f",     "%m/%d/%y",         \
                    "%m/%d/%y %H:%M:%S",    "%m/%d/%Y",             "%m-%d-%Y",                 "%m/%d/%Y %H:%M:%S",\
                    "%m/%d/%Y_%H:%M:%S" ,   "%m-%d-%Y_%H:%M:%S",    "%m-%d-%Y %H:%M:%S"]

当我打印daterange_form时(request.method=="POST"时)我得到了这个:

<tr><th><label for="id_start_date">Start date:</label></th><td><input id="id_start_date" name="start_date" type="text" value="2015/2" /></td></tr>
<tr><th><label for="id_end_date">End date:</label></th><td><input id="id_end_date" name="end_date" type="text" value="2015-10-17_22:10:00" /></td></tr>

在该打印输出中,end_date有一段时间与之关联,但end = daterange_form.cleaned_data['end_date']之后没有时间

任何人都可以帮我解决这个问题吗?

0 个答案:

没有答案