https://www.cnblogs.com/liwenzhou/p/8747872.html

1. form组件的介绍

之前web开发的模式,以注册为例:
  1. 要有一个注册的页面,页面里面要有form表单 --> 生成HTML代码
  2. form表单要能提交数据到后端,后端要做有效性校验 --> 数据有效性校验
  3. 要把校验的提示信息展示在页面上 --> 校验信息返回并展示,保存原来填写的内容

关于校验:
  1. 前端通过JS代码做校验 --> 最好有
  2. 后端做校验 --> 必须要有(因为前端的校验可以被跳过)

2. form组件的用法
  1. from django import forms
  2, 定义一个form类
    class RegForm(forms.Form):
    user = forms.CharField()
    pwd = forms.CharField()
    email = forms.EmailField()
  生成HTML:
  3. 实例化一个form对象, 传递到模板语言中
  4. 在目标语言中调用form对象的响应方法和属性

三种方式:
1. {{ form_obj.as_p }}
2. 单独写
{{ form_obj.pwd.label }}
{{ form_obj.pwd }}

做校验:
1. form_obj = RegForm(request.POST)
2. form_obj.is_valid()

内置的正则校验器的使用
mobile = forms.CharField(
  label="手机",
  # 自己定制校验规则
  validators=[
  RegexValidator(r'^[0-9]+$', '手机号必须是数字'),
  RegexValidator(r'^1[3-9][0-9]{9}$', '手机格式有误')
  ],
  widget=widgets.TextInput(attrs={"class": "form-control"}),
  error_messages={
  "required": "该字段不能为空",
  }
)

重写__init__方法,动态获取数据库数据:

 def __init__(self,*args,**kwargs):
     super().__init__(*args,**kwargs)
     self.fields["......"].widgets.choice = models.xxxx.objects.all().value_list(xxx)

form表单验证流程

  1、正则表达式验证

  2、clean_name()方法

  3、clean()方法验证

整体验证方法(验证两次密码是否一致):

  

 from django import forms
 from django.forms import fields
 from django.core.exceptions import ValidationError,NON_FIELD_ERRORS

 class Test(forms.Form):
     password = fields.CharField(
         label="密码",
         min_length=6,
         max_length=32,
         required=True,
         error_messages={
             "required":"不能为空",
             "min_length":"最少为6位",
             "max_length":"最长为32位"
         }
     )
     re_pwd = fields.CharField(
         label="确认密码",
         min_length=6,
         max_length=32,
         required=True,
         error_messages={
             "required": "不能为空",
             "min_length": "最少为6位",
             "max_length": "最长为32位"
         }
     )
     def clean(self):
         try:
             v1 = self.cleaned_data["password"]
             v2 = self.cleaned_data["re_pwd"]
         except:
             return

         if v1 == v2:
             pass
         else:
             raise ValidationError("密码不一致")

 def test(request):
     if request.method == "GET":
         obj = Test()
         return render(request,"test.html",locals())
     else:
         obj = Test(request.POST)
         if obj.is_valid():
             return HttpResponse("success")
         else:
             return render(request,"test.html",locals())
 <form method="post" action="/personal_web/test" novalidate>
     {% csrf_token %}
     <div>{{ obj.password.label }}{{ obj.password }}{{ obj.password.errors.0 }}{{ obj.non_field_errors.0 }}</div>
     <div>{{ obj.re_pwd.label }}{{ obj.re_pwd }}{{ obj.re_pwd.errors.0 }}{{ obj.non_field_errors.0 }}</div>
     <div><input type="submit"></div>
 </form>

四、ModelForm

 class BookForm(forms.ModelForm):
     class Meta:
         model = models.Book
         fields = "__all__"      #还可以添加很多参数,如label、message_errors、widgits

 def add(request):
     if request.method == "POST":
         form = BookForm(request.POST)
         if form.is_valid():
             form.save()
             return ......

     form = BookForm()
     return .....

 def edit(request):

     obj = models.Book.objects.filter(pk=1),first()

     if request.method == "POST":
         form = BookForm(request.POST,instance=obj)
         if form.is_valid():
             form.save()
             return ......

     form = BookForm(instance=obj)
     return .....
05-11 16:11