码迷,mamicode.com
首页 > 编程语言 > 详细

python---django中form组件(数据添加前使用自定义方法进行验证,以及源码分析)

时间:2018-03-31 22:22:17      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:追踪   唯一性   items   ati   acl   mit   view   返回值   object   

form组件代码:

from app02.models import Userfrom django.core.exceptions import ValidationError

class AjaxForm(forms.Form):
    user = fields.CharField(
        required=True,
        min_length=3,
        max_length=7,
    )
    email = fields.EmailField(
        required=True,
    )

    #自定义方法 clean_字段名
    #必须返回值self.cleaned_data[‘user‘]
    #如果出错抛出raise ValidationError("error...")
  #会在基础验证成功后,使用自定义方法进行验证 def clean_user(self): v
= self.cleaned_data[‘user‘] if User.objects.filter(username=v).count(): #重复了 raise ValidationError("用户名已存在") return v def clean_email(self): return self.cleaned_data[‘email‘]

如果在数据库中发现重复数据,抛出错误。

views代码:

def ajax(req):
    if req.method == "GET":
        obj = AjaxForm()
        return render(req,"ajax.html",{"obj":obj})
    else:
        ret={}
        obj = AjaxForm(req.POST)
        if obj.is_valid():  #在使用is_valid之后才会将数据进行验证
            ret[status]="OK"
            return HttpResponse(json.dumps(ret))
        else:
            print(obj.errors)
            ret[message]=obj.errors
            return HttpResponse(json.dumps(ret))

在is_valid后开始验证代码,从此处进入

源码查看:

class BaseForm:
       def is_valid(self):
        """
        Returns True if the form has no errors. Otherwise, False. If errors are
        being ignored, returns False.
        """若是表单数据正确,则返回true
        return self.is_bound and not self.errors #是属性方法,进行字段验证
self.is_bound = data is not None or files is not None #data传入表单数据不为空,所以is_bound=true

考虑self.errors:

    @property
    def errors(self):
        "Returns an ErrorDict for the data provided for the form"#返回错误信息为表单数据
        if self._errors is None:  #初始self._errors是null    
            self.full_clean()   #进入该方法
        return self._errors

追踪self.full_clean():

    def full_clean(self):
        """
        Cleans all of self.data and populates self._errors and
        self.cleaned_data.
        """
        self._errors = ErrorDict()   #初始化错误字典
        if not self.is_bound:  # Stop further processing.
            return
        self.cleaned_data = {}    #初始化正确数据
        # If the form is permitted to be empty, and none of the form data has
        # changed from the initial data, short circuit any validation.
        if self.empty_permitted and not self.has_changed():  #如果允许为空,并且数据为从初始状态进行改变,则直接返回
            return
     #下面是开始验证的方法
        self._clean_fields()
        self._clean_form()
        self._post_clean()

开始验证字段:self._clean_fields()

    def _clean_fields(self):
     #循环字段,在form组件中设置的字段,该字段来自于DeclarativeFieldsMetaclass的__new__
for name, field in self.fields.items(): # value_from_datadict() gets the data from the data dictionaries. # Each widget type knows how to retrieve its own data, because some # widgets split data over several HTML fields. if field.disabled: value = self.get_initial_for_field(field, name) else: value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name)) try: if isinstance(field, FileField): initial = self.get_initial_for_field(field, name) value = field.clean(value, initial) else: value = field.clean(value) self.cleaned_data[name] = value
         #上面尝试进行正则验证,验证成功后,开始下面逻辑
         #会调用 clean_%s方法name是字段名,这就是我们的自定义方法,我们可以在已经满足正则验证后的数据
         #上再次进行自定义函数的验证,比如验证数据库数据的唯一性处理
if hasattr(self, clean_%s % name): value = getattr(self, clean_%s % name)() self.cleaned_data[name] = value  #注意:最终的值取决于自定义方法:前提是已经存在该方法 except ValidationError as e: #在自定义方法上,如果验证出错,我们需要抛出该错误让该方法进行捕获 self.add_error(name, e)

 

python---django中form组件(数据添加前使用自定义方法进行验证,以及源码分析)

标签:追踪   唯一性   items   ati   acl   mit   view   返回值   object   

原文地址:https://www.cnblogs.com/ssyfj/p/8684354.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!