10 Form表单

一:什么是Form?什么是Django Form

Django的表单系统中,所有的表单类都作为django.forms.Form的子类创建,包括ModelForm

关于django的表单系统,主要分为两种:

  • 基于django.forms.Form:所有表单类的父类
  • 基于django.forms.ModelForm:可以和模型类绑定的Form

案例:实现添加出版社信息的功能

不使用Django Form的情况
建立urls映射,在views里添加逻辑代码
if request.method == "POST":
    # 如果是POST,接收用户输入
    name = request.POST['name']
    address = request.POST['address']
    city = request.POST['city']
    state_province = request.POST['state_province']
    country = request.POST['country']
    website = request.POST['website']
    
    # 这里最好要做非空验证
    Publisher.objects.create(
        name=name,
        address=address,
        city=city,
        state_province=state_province,
        country=country,
        website=website,
    )
    return HttpResponse("添加出版社信息成功")
else:
    return render(request, "add.html", locals())
add.html 内容
{% csrf_token %} 名称:
地址:
城市:
省 :
国家:
网站:
提交:

使用Form的情况

在app里新建forms.py,添加代码:
from django import forms


class Publisher(forms.Form):
    name = forms.CharField()
    address = forms.CharField()
    city = forms.CharField()
    state_province = forms.CharField()
    country = forms.CharField()
    website = forms.URLField()
views代码修改成:
from hello.forms import PublisherForm

if request.method == "POST":
    # 如果是POST,接收用户输入
    publisher_form = PublisherForm(request.POST)
    # 表单验证
    if publisher_form.is_valid():
        # 这里最好要做非空验证
        Publisher.objects.create(
            name=publisher_form.cleaned_data['name'],
            address=publisher_form.cleaned_data['address'],
            city=publisher_form.cleaned_data['city'],
            state_province=publisher_form.cleaned_data['state_province'],
            country=publisher_form.cleaned_data['country'],
            website=publisher_form.cleaned_data['website'],
        )
        return HttpResponse("添加出版社信息成功")
else:
    publisher_form = PublisherForm()
return render(request, "add.html", locals())
实例化PublisherForm(),传入模板中
模板简化成:

    {% csrf_token %}
    {{ publisher_form.as_p }}
    提交:
# as_p p标签 还有 as_table 还有as_ul

使用ModelForm的情况

首先forms简化了
from django import forms
from hello.models import Publisher


class PublisherForm(forms.ModelForm):
    class Meta:
        model = Publisher     
        # fields设置绑定到forms的 exclude设置不绑定的    
        exclude = ("id", )
views也更简单了
if request.method == "POST":
    # 如果是POST,接收用户输入
    publisher_form = PublisherForm(request.POST)
    # 表单验证
    if publisher_form.is_valid():
        # 这里最好要做非空验证
        publisher_form.save()
        return HttpResponse("添加出版社信息成功")
else:
    publisher_form = PublisherForm()
return render(request, "add.html", locals())
哈 少了很多~~~

总结:Django Form可以大大简化代码,常用的表单功能特性都整合到了Form中,ModelForm可以和Model进行绑定,更进一步简化代码


关于Form的验证:

表单-->自定义验证

  1. 表单字段的验证器
  2. clean_filename,验证字段,针对某个字段进行验证
  3. 表单clean方法,可针对整个表单进行验证

案例:自定义验证,不能插入重复的出版社名称

1.表单字段的验证器
from django import forms
from hello.models import Publisher
from django.core.exceptions import ValidationError


def validate_name(vaule):
    try:
        Publisher.objects.get(name=vaule)
        raise ValidationError("%s的信息已经存在" % vaule)
    except Publisher.DoesNotExist:
        pass


class PublisherForm(forms.ModelForm):
    name = forms.CharField(label="名称", validators=[validate_name])
    
    class Meta:
        model = Publisher
        # fields设置绑定到forms的 exclude设置不绑定的
        exclude = ("id", )
2. clean_filename,验证字段,针对某个字段进行验证
class PublisherForm(forms.ModelForm):

    def clean_name(self):
        vaule = self.cleaned_data.get("name")
        try:
            Publisher.objects.get(name=vaule)
            raise ValidationError("%s的信息已经存在llllllllll" % vaule)
        except Publisher.DoesNotExist:
            pass
        return value

    class Meta:
        model = Publisher
        # fields设置绑定到forms的 exclude设置不绑定的
        exclude = ("id", )
3. 表单clean方法,可针对整个表单进行验证
class PublisherForm(forms.ModelForm):

    def clean(self):
        cleaned_data = super(PublisherForm, self).clean()
        vaule1 = cleaned_data.get("name")
        vaule2 = cleaned_data.get("address")
        if vaule1 != vaule2:
            self._errors['address'] = self.error_class(["信息不一致"])
        return cleaned_data

    class Meta:
        model = Publisher
        # fields设置绑定到forms的 exclude设置不绑定的
        exclude = ("id", )

由于clean方法是获取的全部的数据,所有对于判断表单中关系的的字段 尤其好用

你可能感兴趣的:(10 Form表单)