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 に答える