表单处理Django中的日期时间对象格式错误

时间:2013-01-28 17:02:26

标签: python django datetime

我现在已经把头撞到了墙上一段时间了,也许有人可以帮助我。这涉及Django表单和日期时间格式。

所以我试图创建一个表单视图,从用户那里获取一些信息,包括datetime元素。我的观点如下:

def index(request):
un = request.user.username
plot, created = PreciseRequest.objects.get_or_create( user_name=un )
if request.method == 'POST':
  form = PreciseForm(request.POST)
    if form.is_valid():
        plot.user_name = form.cleaned_data['user_name'],
        plot.analyte_choice = form.cleaned_data['analyte_choice'],
        plot.plot_format = form.cleaned_data['plot_format'],
        plot.start_datetime = form.cleaned_data['start_datetime'],
        plot.end_datetime = form.cleaned_data['end_datetime'],
        plot.inoculation_start = form.cleaned_data['inoculation_start'],
        plot.inoculation_end = form.cleaned_data['inoculation_end'],
        plot.time_interval_value = form.cleaned_data['time_interval_value'],
        plot.time_interval_type = form.cleaned_data['time_interval_type'],
        plot.auto_glycerol = form.cleaned_data['auto_glycerol'],
        plot.min_glycerol = form.cleaned_data['min_glycerol'],
        plot.max_glycerol = form.cleaned_data['max_glycerol'],
        plot.auto_methanol = form.cleaned_data['auto_methanol'],
        plot.min_methanol = form.cleaned_data['min_methanol'],
        plot.max_methanol = form.cleaned_data['max_methanol'],
        plot.auto_od = form.cleaned_data['auto_od'],
        plot.min_od = form.cleaned_data['min_od'],
        plot.max_od = form.cleaned_data['max_od']
        plot.save( )
    else:
      plot_data = {
        'user_name': plot.user_name,
        'analyte_choice': plot.analyte_choice,
        'plot_format': plot.plot_format,
        'start_datetime': plot.start_datetime,
        'end_datetime': plot.end_datetime,
        'inoculation_start': plot.inoculation_start,
        'inoculation_end': plot.inoculation_end,
        'time_interval_value': plot.time_interval_value,
        'time_interval_type': plot.time_interval_type,
        'auto_glycerol': plot.auto_glycerol,
        'min_glycerol': plot.min_glycerol,
        'max_glycerol': plot.max_glycerol,
        'auto_methanol': plot.auto_methanol,
        'min_methanol': plot.min_methanol,
        'max_methanol': plot.max_methanol,
        'auto_od': plot.auto_od,
        'min_od': plot.min_od,
        'max_od': plot.max_od
    }
    form = PreciseForm( initial=plot_data )
context = Context({ 'title':'Create a Precise Plot', 'form':form })
return render_to_response('PrecisePlot/index.html',context)

        return HttpResponseRedirect( 'ViewPlot/%s/' % plot.id )

我一直收到以下错误,似乎我错误地传递了datetime对象?

  

[u“'(datetime.datetime(2013,1,1,14,44,27),)'的格式无效。必须是YYYY-MM-DD HH:MM [:ss [。 uuuuuu]] [TZ]格式。“]

任何帮助将不胜感激,谢谢!

模型

class PreciseRequest( models.Model ):
user_name = models.CharField( max_length=32 )
analyte_choice = models.CharField( max_length=3, choices=ANALYTE_CHOICES, default='GMO' )
plot_format = models.CharField( max_length=2, choices=PLOT_FORMATS, default='SD' )
start_datetime = models.DateTimeField()
end_datetime = models.DateTimeField( )
inoculation_start = models.DateTimeField(  )
inoculation_end = models.DateTimeField(  )
time_interval_value = models.IntegerField( default = 1 )
time_interval_type = models.CharField( max_length=1, choices=INTERVAL_OPTIONS, default='D' )
auto_glycerol = models.BooleanField( default=True )
min_glycerol = models.FloatField( default = 0 )
max_glycerol = models.FloatField( default = 20 )
auto_methanol = models.BooleanField( default=True )
min_methanol = models.FloatField( default = 0 )
max_methanol = models.FloatField( default = 6 )
auto_od = models.BooleanField( default=True )
min_od = models.FloatField( default = 0 )
max_od = models.FloatField( default = 2 )
def __unicode__(self):
    return u'%s' % self.user_name
class Meta:
    ordering = ( 'id', )
    verbose_name = ( 'Precise Plot' )
    verbose_name_plural = ( 'Precise Plots' )

形式:

class PreciseForm( forms.Form ):
user_name = forms.CharField( max_length=32 )
analyte_choice = forms.ChoiceField( choices=ANALYTE_CHOICES, initial='GMO' )
plot_format = forms.ChoiceField( choices=PLOT_FORMATS, initial='SD', widget=forms.Select(attrs={'onclick':'showOptions(this);'}) )
start_datetime = forms.DateTimeField( )
end_datetime = forms.DateTimeField( )
inoculation_start = forms.DateTimeField()
inoculation_end = forms.DateTimeField()
time_interval_value = forms.IntegerField()
time_interval_type = forms.ChoiceField(  choices=INTERVAL_OPTIONS, initial='D' )
auto_glycerol = forms.BooleanField( initial=False, required=False, widget=forms.CheckboxInput(attrs={'onclick':'showScale(this);'}) )
min_glycerol = forms.FloatField( min_value=-10, max_value=20, initial=0, required=False )
max_glycerol = forms.FloatField( min_value=0, max_value=100, initial=20, required=False )
auto_methanol = forms.BooleanField( initial=False, required=False, widget=forms.CheckboxInput(attrs={'onclick':'showScale(this);'}) )
min_methanol = forms.FloatField( min_value=-10, max_value=6, initial=0, required=False )
max_methanol = forms.FloatField( min_value=0, max_value=20, initial=6, required=False )
auto_od = forms.BooleanField( initial=False, required=False, widget=forms.CheckboxInput(attrs={'onclick':'showScale(this);'}) )
min_od = forms.FloatField( min_value=-10, max_value=2, initial=0, required=False )
max_od = forms.FloatField( min_value=0, max_value=20, initial=2, required=False )

2 个答案:

答案 0 :(得分:0)

查看forms.DateField上的文档。您可以指定要验证传入日期的格式。

答案 1 :(得分:0)

检查DateField表单小部件。它允许您指定什么格式
日期应该在。

http://docs.djangoproject.com/en/dev/ref/forms/fields/#datefield