1 概念
django表单系统中,所有的表单类都作为django.forms.Form的子类创建,包括ModelForm关于django的表单系统,主要分两种:
- 基于django.forms.Form: 所有表单类的父类
- 基于django.forms.ModelForm: 可以和模型类绑定的Form
使用Django中的Form可以大大简化代码,常用的表单功能特性都整合到了Form中,而ModelForm可以和Model进行绑定,更进一步简化操作。
2 具体实例
# models.py
class Publisher(models.Model):
name = models.CharField(max_length=30, verbose_name="出版社名称")
address = models.CharField("地址", max_length=50)
city = models.CharField("城市", max_length=60)
state_province = models.CharField("省份", max_length=30)
country = models.CharField("国家", max_length=50)
website = models.URLField("网址", )
class Meta:
verbose_name = '出版社'
verbose_name_plural = verbose_name
def __str__(self):
return self.name
2.1 不使用Django Form的情况
# url.py
urlpatterns = [
url(r'^add_publisher/$', views.add_publisher, name="add_publisher"),
]
# view.py
def add_publisher(request):
if request.method == "POST":
print(request.POST['name'])
Publisher.objects.create(
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'],
)
return HttpResponse("添加出版社信息成功")
else:
return render(request, 'add_publisher.html')
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加出版商信息</title>
</head>
<body>
<h1>添加出版商信息</h1>
<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" value="提交"><br>
</form>
</body>
</html>
2.2 使用Form的情况
# url.py
urlpatterns = [
url(r'^add_publisher_django_form/$', views.add_publisher_django_form, name="add_publisher_django_form"),
]
# forms.py
class PublisherForm(forms.Form):
name = forms.CharField(label="名称", error_messages={"required": "这个选项必须填写"})
address = forms.CharField(label="地址", error_messages={"required": "这个选项必须填写"})
city = forms.CharField(label="城市", error_messages={"required": "这个选项必须填写"})
state_province = forms.CharField(label="省份", error_messages={"required": "这个选项必须填写"})
country = forms.CharField(label="国家", error_messages={"required": "这个选项必须填写"})
website = forms.URLField(label="网址", error_messages={"required": "这个选项必须填写"})
# view.py
def add_publisher_django_form(request):
if request.method == "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_publisher_django_form.html', locals())
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加出版商信息</title>
</head>
<body>
<h1>添加出版商信息</h1>
<form action="{% url 'add_publisher_django_form' %}" method="post">
{% csrf_token %}
{{ publisher_form.as_p}}
<input type="submit" value="提交"><br>
</form>
</body>
</html>
2.3 使用ModelForm的情况
# url.py
urlpatterns = [
url(r'^add_publisher_model_form/$', views.add_publisher_model_form, name="add_publisher_model_form"),
]
# forms.py
class PublisherModelForm(forms.ModelForm):
class Meta:
model = Publisher
exclude = ("id",)
# view.py
def add_publisher_model_form(request):
if request.method == "POST":
publisher_form = PublisherModelForm(request.POST)
if publisher_form.is_valid():
publisher_form.save()
return HttpResponse("添加出版社信息成功")
else:
publisher_form = PublisherModelForm()
return render(request, 'add_publisher_model_form.html', locals())
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加出版商信息</title>
</head>
<body>
<h1>添加出版商信息</h1>
<form action="{% url 'add_publisher_model_form' %}" method="post">
{% csrf_token %}
{{ publisher_form.as_p}}
<input type="submit" value="提交"><br>
</form>
</body>
</html>
一、表单字段的验证器
二、clean_filedname,验证字段,针对某个字段进行验证
三、表单clean方法,可针对整个表单进行验证。
案例:
自定义验证,不能插入重名的出版社名称。
网友评论