Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.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 干掉类似的表单类_Python_Django - Fatal编程技术网

Python 干掉类似的表单类

Python 干掉类似的表单类,python,django,Python,Django,我有一个带有简单验证码的python/django应用程序。 根据某些算法,出现了简单的验证码 有两个几乎相同的构造函数。区别就在于继承。 如何避免代码重复 class PostCaptchaForm(PostForm): captcha = CaptchaField() def __init__(self, *args, **kwargs): self.request = kwargs['request'] del kwargs['reques

我有一个带有简单验证码的python/django应用程序。 根据某些算法,出现了简单的验证码

有两个几乎相同的构造函数。区别就在于继承。 如何避免代码重复

class PostCaptchaForm(PostForm):
    captcha = CaptchaField()

    def __init__(self, *args, **kwargs):
        self.request = kwargs['request']
        del kwargs['request']

        super(PostCaptchaForm, self).__init__(*args, **kwargs)

    def clean(self):
        cleaned_data = super(PostCaptchaForm, self).clean()

        success = self.is_valid()
        utils.update_captcha_access(self.request, success)

        if success:
            return cleaned_data
        else:
            raise forms.ValidationError("captcha validation failed")


class ThreadCaptchaForm(ThreadForm):
    captcha = CaptchaField()  

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request')

        super(ThreadCaptchaForm, self).__init__(*args, **kwargs)

    def clean(self):
        cleaned_data = super(ThreadCaptchaForm, self).clean()

        success = self.is_valid()
        utils.update_captcha_access(self.request, success)

        if success:
            return cleaned_data
        else:
            raise forms.ValidationError("captcha validation failed")

我会使用多重继承,将基本验证码表单作为mixin,与ThreadForm或PostForm一起使用

class CaptchaFormBase(forms.Form):  # or forms.ModelForm, if appropriate

    captcha = CaptchaField()

    def __init__(self, *args, **kwargs):
        self.request = kwargs.pop('request')  
        # this is the same as assigning to self request then deleting the key

        super(CaptchaFormBase, self).__init__(*args, **kwargs)


    def clean(self):
        cleaned_data = super(CaptchaFormBase, self).clean()

        success = self.is_valid()
        utils.update_captcha_access(self.request, success)

        if not success:
            # this will put things in non_field_errors, you may want to put it in self.errors['captcha']
            raise forms.ValidationError("Captcha validation failed")

        # always make it easy to see that you're returning cleaned_data
        return self.cleaned_data


class PostCaptchaForm(PostForm, CaptchaFormBase):
    pass


class ThreadCaptchaForm(ThreadForm, CaptchaFormBase):
    pass

这很好,我不担心改变什么