Django的Form主要具有一下几大功能:
- 生成HTML标签
- 验证用户数据(显示错误信息)
- HTML Form提交保留上次提交数据
- 初始化页面显示内容
一,Form验证
第一种操作:主要是这三个函数
is_valid()
cleaned_data
errors
from xiaoqing import models def form(request): if request.method == "GET": return render(request,‘form.html‘) elif request.method == "POST": obj = FM(request.POST) r1 = obj.is_valid() #is_valid() 让这个函数去验证 返回False 或者 True print(r1) if r1: print(r1) print(obj.cleaned_data) #cleaned_data表示返回的正确的信息 # models.Host.objects.create(**obj.cleaned_data) else: print(obj.errors.as_json()) #errors表示错误信息 return render(request,‘form.html‘,{‘obj‘:obj,}) return redirect(‘/xiaoqing/host‘)
<form action="/xiaoqing/form/" method="post"> {% csrf_token %} {# {% for row in error_dict.keys %}#} <p><input type="text" placeholder="请输入用户名" name="user"/><span style="color: red">{{ obj.errors.user.0 }}</span></p> <p><input type="password" placeholder="请输入密码" name="pwd"/><span style="color: red">{{ obj.errors.pwd.0 }}</span></p> <p><input type="text" placeholder="请输入邮箱" name="email"/><span style="color: red">{{ obj.errors.email.0 }}</span></p> <input type="submit" value="提交"/> {# {% endfor %}#} </form>
form.html
第二种操作:如果验证错误,用户信息不会消失
def form(request): if request.method == "GET": obj = FM() #第一次用GET请求时,需要返回客户端一个对象 return render(request,‘form.html‘,{‘obj‘:obj}) elif request.method == "POST": obj = FM(request.POST) r1 = obj.is_valid() #is_valid() 让这个函数去验证 返回False 或者 True print(r1) if r1: print(r1) print(obj.cleaned_data) #cleaned_data表示返回的正确的信息 # models.Host.objects.create(**obj.cleaned_data) else: print(obj.errors.as_json()) #errors表示错误信息 return render(request,‘form.html‘,{‘obj‘:obj,}) return redirect(‘/xiaoqing/host‘)
views.py
<form action="/xiaoqing/form/" method="post"> {% csrf_token %} {# {% for row in error_dict.keys %}#} <p>{{ obj.user }}<span style="color: red">{{ obj.errors.user.0 }}</span></p> <p>{{ obj.pwd }}<span style="color: red">{{ obj.errors.pwd.0 }}</span></p> <p>{{ obj.email }}<span style="color: red">{{ obj.errors.email.0 }}</span></p> <input type="submit" value="提交"/> {# {% endfor %}#} </form>
form.html
二,Form 自定义样式
from django import forms from django.forms import widgets # 需要import 插件widgets from django.forms import fields #用fields 做字段验证 class FM(forms.Form): #字段本身只做验证 它不生成input标签 user = fields.CharField(error_messages={‘required‘:‘用户名不能为空‘}, widget=widgets.Textarea(attrs={‘class‘:‘c1‘})) #用attrs 自定义样式 pwd = fields.CharField(max_length=12, min_length=6, error_messages={‘required‘:‘密码不能为空‘,‘min_length‘:‘密码长度不能小于6‘,‘max_length‘:‘密码长度不能大于12‘}, widget=widgets.PasswordInput) email = fields.EmailField(error_messages={‘required‘:‘邮箱不能为空‘,‘invalid‘:‘邮箱格式错误‘})
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .c1 { background-color: indianred; } </style> </head> <body> <div> {# <form action="/xiaoqing/form/" method="post">#} {# {% csrf_token %}#} {# {% for row in error_dict.keys %}#} {# <p><input type="text" placeholder="请输入用户名" name="user"/><span style="color: red">{{ obj.errors.user.0 }}</span></p>#} {# <p><input type="password" placeholder="请输入密码" name="pwd"/><span style="color: red">{{ obj.errors.pwd.0 }}</span></p>#} {# <p><input type="text" placeholder="请输入邮箱" name="email"/><span style="color: red">{{ obj.errors.email.0 }}</span></p>#} {# <input type="submit" value="提交"/>#} {# {% endfor %}#} {# </form>#} {##} <form action="/xiaoqing/form/" method="post"> {% csrf_token %} {# {% for row in error_dict.keys %}#} <p>{{ obj.user }}<span style="color: red">{{ obj.errors.user.0 }}</span></p> <p>{{ obj.pwd }}<span style="color: red">{{ obj.errors.pwd.0 }}</span></p> <p>{{ obj.email }}<span style="color: red">{{ obj.errors.email.0 }}</span></p> <input type="submit" value="提交"/> {# {% endfor %}#} </form> </div> </body> </html>
form.html
原文地址:https://www.cnblogs.com/sunhao96/p/9003554.html
时间: 2024-10-10 00:37:46