示例:
from django import forms
class FM(forms.Form):
# 根据form表单当中的 name来做
username = forms.CharField()
pwd = forms.CharField()
email = forms.EmailField() # 邮箱格式
pass
def fm(request):
if request.method == 'GET':
return render(request, 'cookie/fm.html')
elif request.method == 'POST':
# 获取用户所有数据
# 每条数据请求的验证
# 成功:获取用户提交的数据
# 失败:显示错误信息
obj = FM(request.POST)
r1 = obj.is_valid() # 判断是否成功
if r1:
print (obj.cleaned_data)
else:
print (obj.errors)
return redirect('/cookie/fm/')
如果有错就打印的是:
<ul class="errorlist"><li>email<ul class="errorlist"><li>Enter a valid email address.</li></ul></li></ul>
我们打印:print (obj.errors)
的话,结果就是:
{"username": [{"message": "This field is required.", "code": "required"}], "pwd": [{"message": "This field is required.", "code": "required"}], "email": [{"message": "This field is required.", "code": "required"}]}
自定制错误信息
from django import forms
class FM(forms.Form):
# 根据form表单当中的 name来做
username = forms.CharField(error_messages={'required':'用户名不能为空'})
pwd = forms.CharField(error_messages={'required':'密码不能为空'})
email = forms.EmailField(error_messages={'required':'邮箱不能为空'}) # 邮箱格式
示例:
将错误信息传递过去:
views.py
class FM(forms.Form):
# 根据form表单当中的 name来做
username = forms.CharField(error_messages={'required':'用户名不能为空'})
pwd = forms.CharField(
error_messages={'required':'密码不能为空', 'min_length':'请填写6位以上密码', 'max_length':'请填写12位以下密码'},
min_length=6,
max_length=12,
)
email = forms.EmailField(error_messages={'required':'邮箱不能为空'}) # 邮箱格式
def fm(request):
if request.method == 'GET':
return render(request, 'cookie/fm.html')
elif request.method == 'POST':
# 获取用户所有数据
# 每条数据请求的验证
# 成功:获取用户提交的数据
# 失败:显示错误信息
obj = FM(request.POST)
r1 = obj.is_valid() # 判断是否成功
if r1:
print (obj.cleaned_data)
else:
return render(request, 'cookie/fm.html', {'obj':obj})
return redirect('/cookie/fm/')
模板中:
<body>
<form action="/cookie/fm/" method="post">
{% csrf_token %}
<p><input type="text" name="username"> {{ obj.errors.username.0 | add:' -- error --' }} </p>
<p><input type="text" name="pwd"> {{ obj.errors.pwd.0 }} </p>
<p><input type="text" name="email"> {{ obj.errors.email.0 }} </p>
<input type="submit" value="提交">
</form>
</body>
</html>
使用正确的方法:
# html
<body>
<form action="/cookie/fm/" method="post">
{% csrf_token %}
<p>{{ obj.username }} {{ obj.errors.username.0 | add:'44' }} </p>
<p>{{ obj.pwd }} {{ obj.errors.pwd.0 }} </p>
<p>{{ obj.email }} {{ obj.errors.email.0 }} </p>
<input type="submit" value="提交">
</form>
</body>
# views.py
class FM(forms.Form):
# 根据form表单当中的 name来做
username = forms.CharField(error_messages={'required':'用户名不能为空'})
pwd = forms.CharField(
error_messages={'required':'密码不能为空', 'min_length':'请填写6位以上密码', 'max_length':'请填写12位以下密码'},
min_length=6,
max_length=12,
)
email = forms.EmailField(error_messages={'required':'邮箱不能为空'}) # 邮箱格式
def fm(request):
if request.method == 'GET':
obj = FM()
return render(request, 'cookie/fm.html', {'obj':obj})
elif request.method == 'POST':
# 获取用户所有数据
# 每条数据请求的验证
# 成功:获取用户提交的数据
# 失败:显示错误信息
obj = FM(request.POST)
r1 = obj.is_valid() # 判断是否成功
if r1:
print (obj.cleaned_data)
# 我们在这里注册用户
models.UserInfo.objects.create(**obj.cleaned_data)
else:
return render(request, 'cookie/fm.html', {'obj':obj})
return redirect('/cookie/fm/')
总结:
Form操作的作用有:
1)验证用户请求
2)生成HTML(同时可以保留上一次输入数据)
Form自定义空间类型和样式
from django import forms
from django.forms import widgets
class FM(forms.Form):
email = forms.EmailField(
error_messages={'required':'邮箱不能为空'},
widget=widgets.Textarea(
attrs={'class':'class01'} # 这里设置类
)
) # 邮箱格式