一:什么是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 内容
<form action="{% url 'add_publisher' %}" method="post">
{% csrf_token %}
名称:<input type="text" name="name"><br>
地址:<input type="text" name="address"><br>
城市:<input type="text" name="city"><br>
省 :<input type="text" name="state_province"><br>
国家:<input type="text" name="country"><br>
网站:<input type="text" name="website"><br>
提交:<input type="submit" name="提交"><br>
使用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(),传入模板中
模板简化成:
<form action="{% url 'add_publisher' %}" method="post">
{% csrf_token %}
{{ publisher_form.as_p }}
提交:<input type="submit" name="提交"><br>
# 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的验证:
表单-->自定义验证
- 表单字段的验证器
- clean_filename,验证字段,针对某个字段进行验证
- 表单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", )