在raise 和 form.add_error 抛出异常的情况中 两种都实用

raise 抛出异常在钩子函数中

点击查看代码
    def clean_domain_ssl_key_content(self):
        """根据协议判断 内容"""
        ###判断协议httphttps
        backend_schema = str(self.cleaned_data.get('backend_schema', None)).strip()
        # domain_ssl_cert_content = self.cleaned_data.get('domain_ssl_cert_content', None)
        # domain_ssl_key_content = self.cleaned_data.get('domain_ssl_key_content', None)
        domain_ssl_cert_content = str(self.cleaned_data.get('domain_ssl_cert_content', None))
        domain_ssl_key_content = str(self.cleaned_data.get('domain_ssl_key_content', None))
        if backend_schema == '1':
            if (not domain_ssl_cert_content) and (not domain_ssl_key_content):
                return domain_ssl_key_content
            # self.add_error('domain_ssl_key_content','您现在添加的是http协议 cert 和key 必须同时为空err')
            raise ValidationError('错误: 您现在添加的是http协议 cert 和key 必须同时为空')
        ###https协议
        if domain_ssl_cert_content and domain_ssl_key_content:
            ###存在都
            return domain_ssl_key_content

image
image

form.add_error 抛出异常

点击查看代码
    def clean_domain_ssl_key_content(self):
        """根据协议判断 内容"""
        ###判断协议httphttps
        backend_schema = str(self.cleaned_data.get('backend_schema', None)).strip()
        # domain_ssl_cert_content = self.cleaned_data.get('domain_ssl_cert_content', None)
        # domain_ssl_key_content = self.cleaned_data.get('domain_ssl_key_content', None)
        domain_ssl_cert_content = str(self.cleaned_data.get('domain_ssl_cert_content', None))
        domain_ssl_key_content = str(self.cleaned_data.get('domain_ssl_key_content', None))
        if backend_schema == '1':
            if (not domain_ssl_cert_content) and (not domain_ssl_key_content):
                return domain_ssl_key_content
            self.add_error('domain_ssl_key_content','您现在添加的是http协议 cert 和key 必须同时为空err')
            # raise ValidationError('错误: 您现在添加的是http协议 cert 和key 必须同时为空')
        ###https协议
        if domain_ssl_cert_content and domain_ssl_key_content:
            ###存在都
            return domain_ssl_key_content

        ##raise ValidationError('您现在添加的是https协议  cert和key 必须同时有内容')
        self.add_error('backend_schema', '您现在添加的是https协议  cert和key 必须同时有内容err')
        # raise ValidationError('raise您现在添加的是https协议  cert和key 必须同时有内容')
![image](https://img2023.cnblogs.com/blog/3040637/202303/3040637-20230308103436496-224559617.jpg) ![image](https://img2023.cnblogs.com/blog/3040637/202303/3040637-20230308103439227-344457533.jpg)
posted @ 2023-03-08 10:34  昆仑葫芦  阅读(0)  评论(0)    收藏  举报