You always have to pay when you come out, but you ca n’t be lazy for a while, and the accounts you owe you will have to be settled sooner or later.
Before being lazy, I did not verify the data entry in the background of the system, but just identified the wrong data in the foreground and urged the second modification; the year-end data inspection found that there was no problem with some data entry but the logical relationship between the data was wrong, without verification, multi-dimensional Verification is difficult to prove.
Well, it ’s time to pay back, re-enter, continue to learn, use django ’s validator function for data entry verification.
参考《Django Documentation》,6.27,validators
- django标准validator,即内建的验证方法(Build-in Validator)
1)RegexValidator
validate_slug / validate_unicode_slug / validate_ipv4_address / validate_ipv6_address
validate_ipv46_address / validate_comma_separated_integer_list / int_list_validator
2)EmailValidator / validate_email
3)URLValidator
4)MaxValueValidator / MinValueValidator
5)MaxLengthValidator / MinLengthValidator
6)DecimalValidator
7)FileExtensionValidator
8)validate_image_file_extension
9)ProhibitNullCharactersValidator
- Custom validator method
1) Reference validators, from django.core import validators
2) Reference validation
error, from django.core.exceptions import ValidationError 2) Custom validator method, called in Form or Model - 调用 validator
1) Build-in Vallidator
2) Custom validator# using in module # format: validators=[Build-in Validator(parameters)] 例: django.core.validators # module contains a collection of callable validators # for use with model and form field''' field_name = models.TextField(..., validators=[MinLengthValidator(20, message='not less than 10')],...)
3)Run in forms:form validation (6.12.7 Form and field validation)from django.core.exceptions import ValidationError # 自定义 validator def <selfdefine_validator(value)>: if value match condition: raise ValidationError("Error Message") # model中调用自定义 validator field_name = models.TextField(..., validators=[selfdefine_validator(), ],...)
4)Run in modelsValidating objects(6.15 Models)# view.py中定义页面元素,并进行验证 from django.forms import Form, fields, widgets
step 1: Validate the model fields - Model.clean_fields()
step 2: Validate the model as a whole - Model.clean()
step 3: Validate the field uniqueness - Model.validate_unique()from django.core.exceptions import ValidationError # models中定义 clean() 方法 def clean(self): field_value = self.field_name # 保存(save)前通过字段名(field_name)获取字段输入值(field_value) ...... if field_value满足条件: raise ValidationError() # 1. 特定的某个字段错误信息提示 raise ValidationError({'field_name':'Error Message',}) # 2. 特定的某些字段错误信息提示 raise ValidationError({'field_name1':'Error Message', 'field_name2':'Error Message', ......, }) # 3. 页面错误信息提示 raise ValidationError('Error Message')