1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
| def clean(self): pwd=self.cleaned_data.get('pwd') r_pwd=self.cleaned_data.get('r_pwd')
if pwd and r_pwd: if pwd==r_pwd: return self.cleaned_data else: raise ValidationError('两次密码不一致') else:
return self.cleaned_data pwd_err=my_form.errors.get('__all__') from django import forms
from django.forms import widgets from app01.models import UserInfo
from django.core.exceptions import NON_FIELD_ERRORS, ValidationError
class UserForm(forms.Form): name=forms.CharField(min_length=4,label="用户名",error_messages={"required":"该字段不能为空"}, widget=widgets.TextInput(attrs={"class":"form-control"}) ) pwd=forms.CharField(min_length=4,label="密码", widget=widgets.PasswordInput(attrs={"class":"form-control"}) ) r_pwd=forms.CharField(min_length=4,label="确认密码",error_messages={"required":"该字段不能为空"},widget=widgets.TextInput(attrs={"class":"form-control"})) email=forms.EmailField(label="邮箱",error_messages={"required":"该字段不能为空","invalid":"格式错误"},widget=widgets.TextInput(attrs={"class":"form-control"})) tel=forms.CharField(label="手机号",widget=widgets.TextInput(attrs={"class":"form-control"}))
def clean_name(self):
val=self.cleaned_data.get("name")
ret=UserInfo.objects.filter(name=val)
if not ret: return val else: raise ValidationError("该用户已注册!")
def clean_tel(self):
val=self.cleaned_data.get("tel")
if len(val)==11:
return val else: raise ValidationError("手机号格式错误")
def clean(self): pwd=self.cleaned_data.get('pwd') r_pwd=self.cleaned_data.get('r_pwd')
if pwd and r_pwd: if pwd==r_pwd: return self.cleaned_data else: raise ValidationError('两次密码不一致') else:
return self.cleaned_data from django.shortcuts import render,HttpResponse
from app01.myforms import *
def reg(request):
if request.method=="POST":
print(request.POST)
#form=UserForm({"name":"yu","email":"123@qq.com","xxxx":"alex"})
form=UserForm(request.POST) # form表单的name属性值应该与forms组件字段名称一致
print(form.is_valid()) # 返回布尔值
if form.is_valid(): print(form.cleaned_data) # {"name":"yuan","email":"123@qq.com"} else: print(form.cleaned_data) # {"email":"123@qq.com"} # print(form.errors) # {"name":[".........."]} # print(type(form.errors)) # ErrorDict # print(form.errors.get("name")) # print(type(form.errors.get("name"))) # ErrorList # print(form.errors.get("name")[0])
# 全局钩子错误 #print("error",form.errors.get("__all__")[0]) errors=form.errors.get("__all__")
return render(request,"reg.html",locals())
'''
form.is_valid() :返回布尔值 form.cleaned_data :{"name":"yuan","email":"123@qq.com"} form.errors :{"name":[".........."]}
'''
form=UserForm()
return render(request,"reg.html",locals()) <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> <style> .error{ color: red; } </style> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> </head> <body>
<div class="container">
<div class="row"> <div class="col-md-6 col-lg-offset-3">
{#<h3>简单form</h3>#} {##} {##} {#<form action="" method="post" novalidate>#} {# {% csrf_token %}#} {# <p>用户名<input type="text" name="name"></p>#} {# <p>密码 <input type="text" name="pwd"></p>#} {# <p>确认密码 <input type="text" name="r_pwd"></p>#} {# <p>邮箱 <input type="text" name="email"></p>#} {# <p>手机号 <input type="text" name="tel"></p>#} {# <input type="submit">#} {##} {#</form>#}
<hr> <h3>forms组件渲染方式1</h3> <form action="" method="post" novalidate>
{% csrf_token %} <p>{{ form.name.label }} {{ form.name }} <span class="pull-right error">{{ form.name.errors.0 }}</span> </p> <p>{{ form.pwd.label }} {{ form.pwd }} <span class="pull-right error">{{ form.pwd.errors.0 }}</span> </p> <p>确认密码 {{ form.r_pwd }} <span class="pull-right error">{{ form.r_pwd.errors.0 }}</span><span class="pull-right error">{{ errors.0 }}</span> </p> <p>邮箱 {{ form.email }} <span class="pull-right error">{{ form.email.errors.0 }}</span></p> <p>手机号 {{ form.tel }} <span class="pull-right error">{{ form.tel.errors.0 }}</span></p> <input type="submit">
</form>
{#<h3>forms组件渲染方式2</h3>#} {##} {#<form action="" method="post" novalidate>#} {# {% csrf_token %}#} {##} {# {% for field in form %}#} {##} {# <div>#} {# <label for="">{{ field.label }}</label>#} {# {{ field }}#} {# </div>#} {##} {# {% endfor %}#} {##} {# <input type="submit">#} {#</form>#} {##} {#<h3>forms组件渲染方式3</h3>#} {##} {#<form action="" method="post">#} {# {% csrf_token %}#} {##} {# {{ form.as_p }}#} {##} {# <input type="submit">#} {#</form>#}
</div> </div> </div>
</body> </html>
|