Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django自定义表单验证/清理_Python_Django_Forms_Error Handling_Cleaned Data - Fatal编程技术网

Python Django自定义表单验证/清理

Python Django自定义表单验证/清理,python,django,forms,error-handling,cleaned-data,Python,Django,Forms,Error Handling,Cleaned Data,好的,我有一个表单,根据下拉列表的值,只需要填写某些字段,但是我得到了一个名为“折扣百分比”的键的键错误,因为如果我将其留空,则在保存已清理的\u数据时看不到它,然后当自定义清理尝试运行时,我得到了键错误,因为它找不到它 def clean(self): data = self.cleaned_data print(str(data)) #try: # if data['discountcode']: # code = data['di

好的,我有一个表单,根据下拉列表的值,只需要填写某些字段,但是我得到了一个名为“折扣百分比”的键的键错误,因为如果我将其留空,则在保存已清理的\u数据时看不到它,然后当自定义清理尝试运行时,我得到了键错误,因为它找不到它

def clean(self):
    data = self.cleaned_data
    print(str(data))
    #try:
    #    if data['discountcode']:
    #        code = data['discountcode']
    #        try:
    #            DiscountCode.objects.filter(discountcode=code)
    #            self._errors["discountcode"] = ErrorList([u"Discount code must be unique."])
    #        except:
    #            pass
    #except:
    #    pass

    if data['discounton'] == '1' and not data['discountitem']:
        self._errors["discounton"] = ErrorList([u"Please select items to apply discount on."])
    elif data['discounton'] == '2' and not data['discountcategory']:
        self._errors["discounton"] = ErrorList([u"Please select category to apply discount on."])
    elif data['discounton'] == '3':
        pass



    if data['discounttype'] == '1' and not data['discountpercentage']:
        self._errors["discounttype"] = ErrorList([u"Please provide a percentage to discount."])
    elif data['discounttype'] == '2' and not data['discountvalue']:
        self._errors["discounttype"] = ErrorList([u"Please provide a value to discount."])


    if data['discountexpiry'] == '1' and not data['discountexpiryuse']:
        self._errors["discountexpiry"] = ErrorList([u"Please provide a redeem limit."])
    elif data['discountexpiry'] == '2' and not data['discountexpirydate']:
        self._errors["discountexpiry"] = ErrorList([u"Please provide a date disable this discount code."])
    elif data['discountexpiry'] == '3':
        pass
    return data
下面是我打印的数据,折扣类型=='1',折扣百分比留空

{'uselimit': u'3', 'discounton': u'1', 'discountcode': u'uyhgkjhg', 'mincarttotal':   u'3', 'discountstart': u'2014-02-19', 'marketid': None, 'discountitem': [], 'uses': None, 'discountcategory': [], 'owner': None, 'discounttype': u'1', 'discountexpiry': u'3'}

感谢所有能帮助你的人,这对你来说意义非凡

如您所说,如果未填写字段,则该字段不会出现在已清理的\u数据中。与其检查值,不如检查是否存在键:

if data['discounton'] == '1' and 'discountitem' not in data:

谢谢你,真管用!奇怪的是,它以前工作过,现在我需要使它们都不需要,这样我的自定义清洁可以接管,我知道,但这从来没有发生过。但无论哪种方式都有效,非常感谢!实际上,我成功地修复了它,并将它恢复到以前的状态,因为当我看到你的修复时,它向我展示了我所做的更改。我需要字段be required=False,然后我的clean方法如何完美地工作:)再次感谢。