2

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

所以我正在尝试制作一个表单视图,该视图从用户那里获取一些信息,包括日期时间元素。我的看法如下:

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 )
4

2 回答 2

0

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

于 2013-01-28T17:05:12.267 回答
0

检查 DateField 表单小部件。它允许您指定
日期的格式。

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

于 2014-08-19T13:22:57.197 回答