【问题标题】:Change django form data before the actual validations take place在实际验证发生之前更改 django 表单数据
【发布时间】:2014-12-17 15:37:45
【问题描述】:

我正在开发一个应用程序,我正在使用时间字段来节省时间,并且我正在使用 jquery 插件来选择时间。我的问题是当我选择插件添加 AM 和 PM 的时间时,django 表单验证返回无效表单。

我尝试了 self.clean_field 方法和 clean 方法从用户提交的时间值中删除 am 和 pm。但没有任何办法。

我的models.py是

class Schedule(models.Model):

    day=models.CharField(max_length=2,choices=DAY_OF_WEEK)
    start_time=models.TimeField(null=True,blank=True)
    end_time=models.TimeField(null=True,blank=True)

    def __unicode__(self):
        return self.day

我的表格是

class ScheduleForm(forms.ModelForm):
    class Meta:
        model = Schedule
        widgets={
        'day':forms.TextInput(attrs={'readonly':True,'class':'day_class_name'}),
        'start_time':forms.TextInput(attrs={'class':'schedule_start_time'}),
        'end_time':forms.TextInput(attrs={'class':'schedule_end_time'}),

    }
time_formats = ['%H:%M', '%I:%M%p', '%I:%M %p']


def clean(self):
    print ' i am in clean method'
    #start=self.cleaned_data['start_time']
    #end=self.cleaned_data['end_time']
    #print start, end
   # print self.cleaned_data
    return self.cleaned_data

def clean_start_time(self):
    #print dir(self)
    start=self.get('start_time',None)
    print start
    #print self.data
    #print ' i am in clean_start_time method'
    #start=self.cleaned_data['start_time']
    #print start
    #if  start and ('AM' or 'PM' in start):
        # start.replace('AM','')
        # start.replace('PM','')
        # return start
    return start


def clean_end_time(self):
   # print ' i am in clean end_time method'
    start=self.cleaned_data['end_time']
    #print start
    #if  start and ('AM' or 'PM' in start):
     #   start.replace('AM','')
     #   start.replace('PM','')
        #return start
    return start

有人可以指导我在实际的 django 表单验证发生之前我必须重写哪种方法以从值中删除 am 和 pm。 帮助将不胜感激

【问题讨论】:

  • 配置你的 jquery 插件,让它为你的表单字段提供有效值不是更好吗?

标签: django django-forms django-templates django-views


【解决方案1】:

您的问题是基于对正在发生的事情的完全误解。您确实不会在cleaned_data 中获得任何带有“am”或“pm”的值。 Django 将字符串形式转换为datetime.time 的实际实例,这就是你所需要的。

你的问题其实是你没有正确设置时间格式造成的。您需要在时间字段本身上设置一个名为 input_formats 的值,而不是在表单末尾随机放置一个名为 time_formats 的列表(它将被完全忽略):

class ScheduleForm(forms.ModelForm):
    start_time = forms.TimeField(input_formats=['%H:%M', '%I:%M%p', '%I:%M %p'])
    ...

现在该字段将接受带有 am/pm 的时间,您无需执行任何其他操作。

【讨论】:

  • 不行,还是不行。formse.errors [{'start_time': [u'输入有效时间。'], 'end_time': [u'输入有效时间。'] }, {'start_time': [u'输入有效时间'], 'end_time': [u'输入有效时间']}, {'start_time': [u'输入有效时间'], 'end_time': [u'输入有效时间']}, {'start_time': [u'输入有效时间'], 'end_time': [u'输入有效时间']}, {' start_time': [u'输入有效时间'], 'end_time': [u'输入有效时间']}, {'start_time': [u'输入有效时间'], 'end_time': [u'输入有效时间。']}, {'start_time': [u'输入有效时间。'], 'end_time': [u'输入有效时间。']}]
  • 请求,POST 是
  • 好的,所以你可以从中看出时间毕竟没有上午/下午,但他们确实有秒。所以你的格式应该包括"%H:%M:%S"。
  • 是的,非常感谢丹尼尔。这是几秒钟的错误。在格式中添加秒后工作。
【解决方案2】:

以下应该有效。覆盖 clean 方法。

class ScheduleForm(forms.ModelForm):
    def clean(self):
        cleaned_data = super(ScheduleForm, self).clean()
        # Here you remove what you need
        return cleaned_data

【讨论】:

  • 当我在 clean 方法中调用 clean_data 时,它会在执行验证后返回数据
  • cleaned_data 是 {'close': False, 'day': u'MO'} {'close': False, 'start_time': None, 'day': u'TU', 'end_time ':无} {'close':False,'start_time':无,'day':u'WE','end_time':无} {'close':False,'start_time':无,'day':u 'TH', 'end_time': None} {'close': False, 'start_time': None, 'day': u'FR', 'end_time': None} {'close': False, 'start_time': None , 'day': u'SA', 'end_time': None} {'close': False, 'start_time': None, 'day': u'SU', 'end_time': None}
猜你喜欢
  • 2013-09-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-09
  • 2018-02-17
  • 2011-10-22
  • 2021-09-08
相关资源
最近更新 更多