一:表单验证:

场景:因为浏览器的js可以被禁用,所以需要做后台的输入合法的验证。

A:ajax发请求。需要注意的是ajax POST的数据的key值和form表单的里的字段名字一致,否则得不到验证!!!

通过ajax发请求给后端提交数据,然后后端通过form进行输入的验证。

之前ajax POST数据的时候后端需要做如下:

 def login(request):
if request.method=='POST':
user=request.POST.get('user')
pwd=request.POST.get('pwd ')
print(user,pwd)
return HttpResponse('ok')
return render(request,'login.html')

1:用form做用户验证的的时候,不需要做这些,直接将request.POST 传入form对象即可获取所有POST数据。

 class Loginform(forms.Form):
user=forms.CharField(required=True)#需要继承forms.Form这个模块
pwd=forms.CharField(required=True)#require=True表示该字段不能为空。 def login(request):
if request.method=='POST':
form_obj=Loginform(request.POST)#获取浏览器提交所有数据
if form_obj.is_valid():#检测POST过来的数据是否符合我们form定义。返回值是bool
data=form_obj.clean()#通过对象的clean()方法来获取用户的提交的数据。
print(data)
return HttpResponse('ok')
else:
error=form_obj.errors##通过errors()方法 来获取我们的提交的数据错误提示。这个方法加了装饰器@property变成字段。
print(error)
return HttpResponse(error)
return render(request,'login.html')

前端页面代码:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script src="/static/js/jquery-1.12.4.js"></script>
</head>
<body>
<div> <input name="user" type="text"></div>
<div><input name="pwd" type="password"></div>
<div><input type="button" value="提交" onclick="Submit(this)"></div>
<script>
function Submit(ths) {
var data_list={};
$('input').each(
function (i) {
var name=$(this).attr('name');
var val=$(this).val();
data_list[name]=val;
}
);
$.ajax({
url:'/login/',
type:'POST',
data:data_list,
success:function (data) {
console.log(data)
},
error:function (error) {
console.log(error)
}
})
}
</script>
</body>
</html>

输出结果:

 {'pwd': '', 'user': ''}
<ul class="errorlist"><li>pwd<ul class="errorlist"><li>This field is required.</li></ul></li></ul>

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAdMAAACDCAIAAABZZWa5AAAPTElEQVR4nO2dTW7cOBpAeazmPewjDE+QveoAsYCGDe2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K8ZBsADucKzRtniSBnkZ5esl2xTNPufJtbU3RLuYcyAyP7ZOULujXRws1DKwf1p+dkuwtdf6oIXpwWyvwiXoBVXJt54XC4qgzgUDAvrIa/pAA4EMx7JrDsEMAFgXkBAGKDeQEAYoN5AQBic+rmBQC4PDAvAEBsMC8AQGwwLwBAbDAvAEBsLsa8lRLZSf0t1QaLCQHAhYJ5j8shN5kEgEvlhM1bl1IkQuxUUan2gdZa6yIXYtf8dGsxtDXbH1n+WzdNsrQ29fMi1Oc//tinMhEiS4vmqTl9x1pMKDwlgeV8upWFiuY5s5LEsNBd+WequV1TyaMs5gZw1ZywebXWutHiTqhKF3n7r5Gj/TgQ81oldSkb8wb71JUSmVRl3fQpywn3xlxMyGdsOZ9WnzKtvRvreoXWnXVr5/7mw+Z9N/26QACwGedgXlWZ3wtlxbZiJ8ROpnut9WrzWn1qXSmRWOFzPiWaiIsJeYwt56NH7hfmF7q/197yGMNlLrkpD8DROAPzuveCTUZi0nXmdW0yVnOS4y8m5DN+P8btzWvXY6UfgK05M/M2SduuxIpVrZqFSmS6t31aqERsY96oiwkNGL0H+SLzuuv62AsFBWraCV/MC7A1J23eOs3MSbPeDNYZtnC5KexKVFpK0QbLgz67tK/IC/N4NNUbbTGhEYbL+bg5iMB6wlZWIrDMcLhmaNUgANiOkzYvAMBFgnkBAGKDeU8PFv4BuHQwLwBAbDAvAEBsMC8AQGy2NC8AACwB8wIAxAbzAgDEBvMCAMQG8wIAxAbzAgDEBvMCAMQG8wIAxAbzAgDE5v8HKwjYdMLRigAAAABJRU5ErkJggg==" alt="" />

输出打印error对象的类型:

 <class 'django.forms.utils.ErrorDict'>

可以看出他是一个类对象,我们导入这个对象查看下他的方法:

             error=form_obj.errors##通过errors()方法 来获取我们的提交的数据错误提示。这个方法加了装饰器@property变成字段。
from django.forms.utils import ErrorDict
print(type(error))
return HttpResponse(error)

查看源码:

 @html_safe
@python_2_unicode_compatible
class ErrorDict(dict):
"""
A collection of errors that knows how to display itself in various formats. The dictionary keys are the field names, and the values are the errors.
"""
def as_data(self):
return {f: e.as_data() for f, e in self.items()} def as_json(self, escape_html=False):
return json.dumps({f: e.get_json_data(escape_html) for f, e in self.items()}) def as_ul(self):
if not self:
return ''
return format_html(
'<ul class="errorlist">{}</ul>',
format_html_join('', '<li>{}{}</li>', ((k, force_text(v)) for k, v in self.items()))
) def as_text(self):
output = []
for field, errors in self.items():
output.append('* %s' % field)
output.append('\n'.join(' * %s' % e for e in errors))
return '\n'.join(output) def __str__(self):
return self.as_ul()

我们可以看到当我们print的时候输出的as_ul。错误信息输出有三种数据格式:

1、ul格式,就是之前我们输出的样式:

 <ul class="errorlist"><li>pwd<ul class="errorlist"><li>This field is required.</li></ul></li></ul>

2:json格式。as_josn.

3:as_text.文本格式。 我们根据我们的需求来获得我们想要的格式。

下面用as_josn来获取我们的错误输出。

 def login(request):
if request.method=='POST':
form_obj=Loginform(request.POST)#获取浏览器提交所有数据
if form_obj.is_valid():#检测POST过来的数据是否符合我们form定义。返回值是bool
data=form_obj.clean()#通过对象的clean()方法来获取用户的提交的数据。
print(data)
return HttpResponse('ok')
else:
error=form_obj.errors##通过errors()方法 来获取我们的提交的数据错误提示。这个方法加了装饰器@property变成字段。
print(type(error))
error_msg=error.as_json()#获取错误信息json格式。
print(type(error_msg))
return HttpResponse(error_msg)
return render(request,'login.html')
 {"pwd": [{"message": "This field is required.", "code": "required"}], "user": [{"message": "This field is required.", "code": "required"}]}

因为传输的过程是字符串传输,可以在ajax出添加datatype:json来获取json串不需要进行转换。

我们自定义字典,方便我们取值。如上结果我们真正关心的是:“pwd”:[{"message": "This field is required."}]对于code的信息我们不关心。所以定义字典data_dic={"status":false,message:{None}}.

code:

 def login(request):
res={'status':False,'message':{}}
if request.method=='POST':
form_obj=Loginform(request.POST)#获取浏览器提交所有数据
if form_obj.is_valid():#检测POST过来的数据是否符合我们form定义。返回值是bool
data=form_obj.clean()#通过对象的clean()方法来获取用户的提交的数据。
print(data)
res['status']=True#符合form规则为true
return HttpResponse('ok')
else:
error=form_obj.errors##通过errors()方法 来获取我们的提交的数据错误提示。这个方法加了装饰器@property变成字段。
error_msg=error.as_json()
print(type(error_msg))#as_json转换过来的数据是字符串类型。
print(error_msg)
res['status']=False#不符合form规则我们定义status为Flase。
err_msg=json.loads(error_msg)#将字符串格式的json转换成字典格式,方便我们取值。
for key,val in err_msg.items():
res['message'].setdefault(key,val[]['message'])#循环取‘user’,‘pwd’对应的报错信息。
print(res)
return HttpResponse(json.dumps(res))#将json格式的转换成字符串返回给浏览器客户端。
return render(request,'login.html')

如上所示我们需要给字符串格式的json转成我们处理的数据类型,并取我们想要的值给浏览器返回。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgcAAAArCAIAAABjMrwIAAATuElEQVR4nO2d+1MT2Z7A/Qf2l2uVt4oatbJVlOV6b+FuyTgz5dS4s05p6ep6uTrO6PiYGkUewiDCgKAuIiriCHJlcFHHEQYGQY0PRBEExUt4RF4Jj7yaAAmQkITOs7vTdLrP/nC0aUMSAXFkrPOp/NLpx/d7nt/HOZ3MAwgEAoFAvGTeu1YAgUAgEHMIZBUQCAQCMcE8GwKBQCAQL0GxAgKBQCAmQFYBgUAgEBMgq4BAIBCICZBVQCAQCMQEyCogEAgEYgJkFRAIBAIxwZtahYsXLxqNxllRBYJh2Pr162Uy2Sw+8w8NSxLquJihn/Jm5Wkcw+jOncUO/8DR9Kw88J2ImDr2Rols43q3bvBdK/KeQGrUHV/8p7OzY1aeNrt9+12JmCJzalwE5k2tQmxsbFxc3M2bNwmC8HeNSqXauXNncHDw9u3b+/r6AAAdHR0ZGRkMw0y+WKFQbN68WaFQTEsNhmFycnLa2tpeeyXHcY8fP/7888/j4+Pdbve0pEBsA/Z7+6vctmncO3jmtDbtmKunR7ZpA9XfT2JY68ehDUEL+A9eU83RdN+RFFeX3Otej8uljNxvKLo2A1U5hhktL5OuWN4QtAAODI5h+jOO92cc53xVPs+42aSM2DduNvk8a6m4p8/NCSB0KiJ+H6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IBAIBGICZBUQCAQCMQGyCggEAoGYAFkFBAKBQEyArAICgUAgJkBWAYFAIBATzIJVsJiYxH26Jf8i+yJEgSl9/x2VuBj/YF7HB/M6pA3v7W+hIBAIxHvALFgFcTFemG+eyu8v5aQbkFVAIBCIucwsWAWvuR5Tur8IUXwwr2Pl4u5bv+Iej+8rOQ48vG3775WqD+Z17FiL4RYPAIAiuSvnTSsXd6/5q+JOKT7HfoIQgUAg3n/eyCrweSH4yUl/5XebrbjnaIxePzDxp1FCq4BbPAf3DArPAgAeiG23fsVZFlhxT0qUvrvjfftBUwQCgZjjzH6sYDExyRG6v/xJ/sG8Dq+VBuGVbjeXfdwQ/fVA4xMnRcK/8OTSDg4JzQxKNyEQCMTvzOxbhUvZppx0g9vNUSSXkTjszyoAAFgW6AfoS9mmr9ZgWrWborj0Q8O9srf4/woIBAKBCMzsW4ULp4yF+ebxce7xffvW1ZoAVgHCMOBM6gj8/rcrlsyUEYedBQgEAoF4F7yV1eZt/6X5y5/kx+OHEvfqoFXISTfweSGYVhIuSmenGQgXCwAgXGx2mmHl4m7hEjQCgUAgfjfQW2wIBAKBmABZBQQCgUBMgKwCAoFAICZAVgGBQCAQE/w/MCNf61iVPN4AAAAASUVORK5CYII=" alt="" />

需要注意的是:ajax中指定返回数据类型是dataType是大写的T!!!!!!

页面输出我们错误提示:

前端页面代码:

 <body>
<div> <input name="user" type="text"></div>
<div><input name="pwd" type="password"></div>
<div><input type="button" value="提交" onclick="Submit(this)"></div>
<script>
function Submit(ths) {
var data_list={};
$('.error').remove();#每次提交的时候,需要删除错误提示。
$('input').each(
function (i) {
var name=$(this).attr('name');
var val=$(this).val();
data_list[name]=val;
}
);
$.ajax({
url:'/login/',
type:'POST',
data:data_list,
dataType:'json',
success:function (data) {
if(data.status){
void
}else {
$.each(
data.message,function(key,val){
var tag=document.createElement('span');
tag.innerText=val;
tag.className='error';###注意不要用id因为一个html文本中id是唯一,他只能删除第一个。
$('input[name='+key+']').after(tag) }
);
}
},
error:function (error) {
}
})
}
</script>
</body>

效果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAb0AAABYCAIAAADusr6WAAAINElEQVR4nO3d+1cTZx7H8e8f3HO2aFvBbbcCpftLdS03rYLKTRMk1tNWkgAJVF1dCAokkNA9u7pnWblG1gv2tyX7Q3rmpDOZ71ySSQbyfp0czzPJM89lzpMPMwMmksrtTaazXd+OvHv3rgQAcCJz2d3J1Grn5VvkJgC4IXPZnUhqtfPyTXITANyQ2exONLV6gfNNAHCnfJ2+1qne34wC0Wgj1yUQZjKX25tM57r7RvXcbOSYEEKsAcAgqfX9u/PrX/WPkZtQsAYAg6Q3ilM/53sGJshNKFgDgEHS+ddTDws9g3fITSii0ahUUyqVjEJV+qtembr22r5pX6WOy6Zc9ttEdR/nSZl4oGQ+/3rq4ebXVyLkJhTl3CyXlYJVHd9mle34y01rO/4q+Ou3KYIY54mYeKAknT+MPdr8M7kJVeUasL5tGvNGCiJ/A+3otOL4yHzhcOoR55twQG7CwPGRdJ7chDM3uWm9e6jcT7TeJ1VUva/qvv2qo/W0i13NWuZVWUc5dFXHb93X9Koyznr128p+y80echMqx9w0vfdMr1rr6xWqUvLOU/t2eVf7eHzMyxRzju1ULfubb+39tiyZzx/yeyE48nSd7jU3XfKUmyaexlOv3PTXSEk9v666b40/J3z327IktcHv0+GsvrlZ8nXR5/t802v9Ws5/vc7LfahZnw8iNz3127IktfH67sPNnkFyE5q656anCnq1UOWm13bIzZNI5jaKkwubPQP83Ts0gV6nB52bnnKkllNFH/Nyk1+Vm1XLpifrlZt2ffk4VqdMOTcL3eQmVMYasF6EWu+FGZvye9ZdXL7xfLTjo1/TsOs7HveNKON3MzXrREyVa+zX04E6rWR2vTi5kO8euE1uQsEaCKHWzKwwkJn1YpTchBPWQNi4PKtFEGQmdxBdyHf183lI0LAGAIMkcwfR+XwX55tQsQYAgyRzxQi5CSesAcAgyVwxspDv6nfITaCR6xIIM0nmDiLzhU6n802gkesSCDNJ5A7uLBS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Ykvbw3GH36efHVg+UImIXsGumpsdyU0AoSL3MjvDiWzHJe3vN8/0Z/ZV5bNLo1Cpso7xr90zVZ0dWCY3AYSH3Mvs3kjmztv///Rybu7aMPLR9KRdfeNVvU4lchNAqMj3y3u3ZtY/v2z7+ZsTT4/b+pZ2VCJSWTDC1LG+G2f6M+QmgPCQ+8/2R2Y3vnDKzW2ViFQWjE2lsl6nErkJIFTkh5XiqPq9bBNPjz/uXfyPSkQqC8amtVrlS6ZNO219S+QmgPCQH1eKo6lN5XuAy7m5Za8cf+WCsWkw1VF2t0NuAggV+Sl7OJ7e/LI/oufmv5uH3AQQKjKdezOx8PcLA5NKbv7h27/9q3k+7l0kNwGEh8TX397++R+dg3f13HzZPOQmgFCR+MbbyMN/dl2Z0q/TXzQP1+kAQkUS+XeRRy+67XMzt3X80c03Z/ozbX1LjX+c6c98dPMN3y8EIDwkkX8ffazlZqlUym0dTzxt2oPQBBAqkiy8jz5+0X01puQmAMAgyTy5CQAekJsA4A25CQDeSCJ/RG4CgHuSLJCbAOCBJAtHk49ffEVuAoA7kiwcTf6V3AQAt2SmnJvfkZsA4Aq5CQDe/JabPeQmALgjs5vkJgB4QG4CgDcyu3l098lLchMAXJK5cm5eIzcBwBVyEwC8kblfjqaevPya3AQAd8hNAPBGUuQmAHghqV+OYuQmALj2f8rLvreuH1hzAAAAAElFTkSuQmCC" alt="" />

如上代码行:8 需要用class来删除。因为如果用id进行删除的时候会出现:只删除第一个。而且id是唯一不能多次命名,多次提交会出现如下错误:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAuMAAABVCAIAAABsE32yAAAH9klEQVR4nO3d24/cVh0H8PMHV2qSXmiEUtLSJG0aBLm32bQVpAEGIRAS6mMfSZP0QhOeAbEIEAgoi8IbOzysOji+/M6xxzN2dj4fWZVn5vhcnN96vvFMtunVS7cODg4++uVHNpvNZrPZbHPb0plLtw4ODpYAAPMjqQAA85XOvP2upAIAzFM687Z7KgDATPn0BwCYr3Tm0p6k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uXOHXtoaQCALssn1T+FEopVXdWD4PGcZsqSQUAdlwmqZy4cv+PoZRSdWf1sNms+lLtYZeTVx9IKgCwy/JJZb/bUeA42lk9XKm1CQ7vIqkAwI7LJ5U/TEdSAYAdl86HSeX5y5/8fjonrtyXVABgl2V+m/7zlz/53XQkFQDYcZl7Kieu3P/tdHz6AwA7Lkoqj/cPn/vgq1PXHp68+mD726lrD5/74KvH+4dbPiMAwHxESWW5XD7eP7x7b7JNTAGAHZcuhEkFAGBCmXsqAAATklQAgPmSVACA+ZJUAID58o1aAGC+JBUAYL4kFQBgviQVAGC+JBUAYL4kFQBgviQVAGC+/gdcWehoVv9UbgAAAABJRU5ErkJggg==" alt="" />

2:错误信息重写:

一:常规定义错误提示。

错误提示是英文。我们需要把英文错误提示转换成英文提示。

可以在我们后台form类中定义中文如下:

 class Loginform(forms.Form):
user=forms.CharField(required=True,error_messages={'require':"用户名不能为空。"})#需要继承forms.Form这个模块
pwd=forms.CharField(required=True)#require=True表示该字段不能为空。

因为现在的错误提示是require做的限制。(required=True)。通过变量error_messages来定义错误。字典中key和我们做限制的参数名字一致。

效果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAX8AAABQCAIAAAB+q+JnAAAGeUlEQVR4nO3dzW/cRBjH8ecPrkSTtM0mtDS7aVqKmpBmd5M0EUrbNLTLyw312GNDk7SlCWdABAECAWVRuLHLYSXL2J5nZvwa7O9HVuWX8Xi8Gv80drxb+WBrMASA0snte0+qbgOAJpLbm6QPgArI+xukD4AKyK2Nx8Ph8OkXT5mYmJjKnOTm+t5wOBwDQLlkqU/6AKiA3Ojukj4AyieL3YekD4DySefuA9IHQPmkvXqf9AFQPmmv7pA+AMonC4x9AFRB2upznwEwGJTZI9Eclr950fOQSx8QkULLp67W/UBeTSqo/TUjN3ra+z6kD4I+IDZ6PflevdbGxJsULIbXm+ati9lPAZZ3nUkfhNMnvN734vQaZfhGm/UQ1iRK3FfJJpc2pz6RhpCl9cekDxRZ0ifFdW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QB1AQaa3skj5Q0AdQEEv6rO0+e/ejr6ua1nafFflrxXBVZo9Ec8jssjF9Tk5HU7tnc9vHra035U9z28dTu2cnp6OSPxEA5ZDW8kNT+uztj1pbb/6uztz28aPn/5T8iQAoh8yp6XNl8/VZdWbvfUX6AHUlreUHSvpc3nj1V3WubL4mfYC6ktk7lvQZqkQkmAnTCzsifYAak5Ytff5UiUhkJjJvKu+C9AFqzJI+l9Zf/qESkWAmLFxAp1R+eeMV6QPUlczeua+nz+8G8RwJbzLt5YX0AWrM8txnpn/0m0pEgpkwpWRkXnFp/SXpA9SV5W9eM/2jX1UiohcwFXPZkfQBakzmVrT3faZ7h7+oJiOdYCYsXCb417Qm0Uz/iPQB6krmzd/zmqTPzwZBykRWmsoHW/UyYaQPUGNyddX4+z57+6Op7sFPKhEJzwSRZC3vYrp3SPoAdSXXbOnzo0pEwjPBolJYLxNG+gA1pv2q/N7+6OLaix9UIhKeCRbjxcKbIosmU90D0geoK+1/1Jmkz6nZJEQmM8FiIFJG2d2E9AFqTK73jP+b4CR9vq8O6QPUmCz0P1HS5527X35XnYtrL0gfoK6kvf6pnj7fVof0AWpMOhuf6Xde31SHOy+gxmTRnD4np6MLO2+ne4dT3YPyp+ne4YWdt/yuM1BXWvqMx+OT09HefmUT0QPUmCxufq6kDwAUhPQBUA3SB0A1/gWyQ4KdPqxzgwAAAABJRU5ErkJggg==" alt="" />这是对之前的错误信息重写。

密码错误提示重写。

 class Loginform(forms.Form):
user=forms.CharField(required=True,error_messages={'required':"用户名不能为空。"})#需要继承forms.Form这个模块
pwd=forms.CharField(required=True,min_length=,
max_length=,
error_messages={'required':'密码不为空!',
'min_length':'不能少于6位!',
'max_length':'长度不能超过10位!'})#require=True表示该字段不能为空。

效果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAUkAAABICAIAAAD0w5DuAAAHS0lEQVR4nO3dW1MTZxzH8f+76KWXfQm99LKXvgQve+NMBQ+AVQs4SNsRDyEBKwflEEKUoFVJ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NbgzCLzGcb7bS/r2090f6N4sahkDHN3PWgbwAR6mtweCixetuv+2wP7bf9dPWgbwASaSL1zBn9rdriZtr++9eur6jlti6BtAKPIm37vDP3e7LjPt71UPWgbwATypt+P/PwH3/ZpW+TP6sFrcgATyJvZGZnl2k4tF07dfVdvj9Z1zlX+q94ePXX3HT4vDcAo8mV2Rp6/bHY8YD7nNLVccIar9oWwAUwgf2bXNfuyhW0bACyH/Old16xg3QYAyyF/encEbQMcOWgb4GgifwZtAxxB/wGT12sGiTuR/AAAAABJRU5ErkJggg==" alt="" />

二:对django自己做的错误提示重写:

我们添加一列数字,默认required=True。

 class Loginform(forms.Form):
user=forms.CharField(required=True,error_messages={'required':"用户名不能为空。"})#需要继承forms.Form这个模块
pwd=forms.CharField(required=True,min_length=,
max_length=,
error_messages={'required':'密码不为空!',
'min_length':'不能少于6位!',
'max_length':'长度不能超过10位!'})#require=True表示该字段不能为空。
num=forms.IntegerField()

效果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAaMAAABfCAIAAADK0D91AAALM0lEQVR4nO3d+XMT5x3H8ec/aVpwmxp6OPxbmSmQk+nQkjQcIem03AkGMrXjggiYa4zDBGPLtiRbsjlsKMYY67DapCUELNkGER6pP6wkr/b4PseujlU+r9EwOp495Fm/51kdmP2sY9PyauHr6czgVPpqPNk7MLHz0+O5XK4EANAuWLl0U5nBqdTVeLJnILbzk89ROgBoJ+ulu5pIDcQXewZiOz/BnA4A2kq5dINT6auJ1IAxp8PZKwC0l0rppjNXp1IDiWTPwOTOT7tROgBoJ5Wz15tLg9OZwal072D8T389gdIBQDthr/1y8/Lay2u3/33tVvbrm9m+a9O7/nYKpQOAdsJe+9Vvcms/Xp/57zcz331z59szQ7c/OPAPonQhgFCokccogHfs56//Nvf8xxv3vr9x7/GNu4/PDs/+5VAvXbpG7h+0IF+OAcZYXcdrr1Z+Q0q7VKf9B0nsF6//Lv/8VfjBs/Dcs5H7T8+N3t995J8oHRCqxwATodfjbymEO2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HtbI1aloYU3l3rd2/JpueN5MP+/Kr9GAFBNfJUmPet63+ZM4NXo/Itvb2spVfrslnNLPs23XagSQG0TVMmbMQS2Rud/V/bSqvRpz9JL+Xx2y4kqAdS2fJXc63pvxhCUVKUMFSGkMCFEX1giVAmg5vFVmnKv632ZGWlV2qQihOyb2DcttrwUqBJAzStVpTu0Kn1yczFNRQgpTAgJF6CjPPinPUuoEkBty1fJk9L7MoblXJXG6FVKiSjui/AmsXtVBFUCqHn7q/RLuSp93O1YpyKEFCaEKEvum6b45OYiqgRQ2/aqNJ2vUmu5KiWoCCH0BcQWk3JHVAmg5vFV0nlS0/7M7PJ22Spd7LLHqXJ7RoUJIeEyhX/F5pT0cbcDVQKobfurpJJQpaiIQn32zRRbvnArfRkhVAmg5uWrxKVm8lVq66NV6UKnLUJFCBFOFFJVdnkpLnbZUSWA2sZXSc+lZvwZY65K1H2lC522MBUhRDhRuEpZmL6MEKoEUPNKVon2eaXzHdZVKkKIcKJwtXgx4U37roq50GlDlQBq216VDIGMcXlLPf97G/Wz3ec7rCvicnHJTRSuFuxbhnJ3MagSQM0jx098sLm9O82lDP5clV609tK+yeR8h/W/8kGVAGoeXyW9N1elV+r5UGuv6Le+DS28OfeP5y/kc77DiioB1DZhlTaNy69U8yHl7TJVCskHVQKoefkjuFyVgi9V878pb/1CP4L7TT44ggOoeYXzSmm+Ss9+VfaI/jUBbuXNRwMbF7vsFzpt1b9c7LJ/NLCB7+0GqG18lWa86Vn/RtkqZbNZbuXN0IJsFyQJoObtVcng3zAFX6qe/YdeJQAApt7aVzIFX6pRJQCQlaBKgQ1T8KV6HlUCADn9H5yTvumg2FlrAAAAAElFTkSuQmCC" alt="" />

我们可以在我们自定义form类中重写这个错误提示。

 class Loginform(forms.Form):
user=forms.CharField(required=True,error_messages={'required':"用户名不能为空。"})#需要继承forms.Form这个模块
pwd=forms.CharField(required=True,min_length=,
max_length=,
error_messages={'required':'密码不为空!',
'min_length':'不能少于6位!',
'max_length':'长度不能超过10位!'})#require=True表示该字段不能为空。
num=forms.IntegerField(error_messages={'invalid':'请输入数字!','required':'输入不能为空!'})

效果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAS4AAAB5CAIAAAAI3lvvAAAMVUlEQVR4nO3cyVtT5wLH8fe/YGlx0bC41GWXLl26dOnybvo8Vjuove1t1YvUXpVeBLEK4gxYkjAnkBFQIQNkOJmAauwFMpMJAo/Qa+4iJ+FAyJsT5JxI8vs8eXxOTt6EgO+X92QgpFZSv7qxNe4KT3hWXszHBl7N/XK3Jwl7MRgMWq323bt3aYCDRmol9UmkyJvBYJDJZD0AB21HipNIEaBMdqY4F+t/NXftt+5y3yuAqsOmqGdTjPa/8ly7gxQBxJZN0Rked0cmPNH+l56f25AigNhyKYb07siEO9o36Wlse5ZMJkeGRnDCCSfRTmyKOkdI7wqPuyLyCVdj69NkMlnup3YBqgupldQn17e0jqDeEdI7w3K94+qtx0gRQGTZFJmgngnqmYBMY2v49QFSBBBZLsWA1u7XWX29KkvDzQ6kCCAyUvtpfXJ9S2v3a63LutnFXqW54fpdpAggMlL76d+S65tam187u6Q1/9k7Ymi41oYUAUTGpqix+bQzi1rT296hqYafbyNFAJFlU7T6NLPZFBuRIoDYOKuiJXuA+ssdSorlfv86fBTEnKNVIvO0zaba5tdafbrZpV6lueH6PXqKYt4/+AhhDggh82LGpsYW0Nr8WqtPqrZcbbqPFIECc0AI7OuKGntQaw/o7EGp1t7Y/BApAgXmgBCyKTJBDRPSOcJSvfNqC+09qPhvAMwBIbAHqGomqHGEdc6IdMLdeLurhBS9XSdPdvkFv5/wEUGKQiC1kvrE+qaaCWkcYZ1rRTrhaWzr5puiV/H3zwlBilUGKQqBk6IzonNHeyc9V/mlGNM1Hj9y/Pz500ix2iBFIWRS3FLZg2pHROsqJUXLlDuWTk81IsVqgxSFwE0xnEmxgf8BahopViOkKIQdKepc0d4JvqsiCylWH6QoBPaxosoWVDvCGucKUoSikKIQsinagypHWONc+X3C03AbKQINUhQCm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BFAHE9n8j6cvUcZ30zAAAAABJRU5ErkJggg==" alt="" />

通过:print打印出我们的错误信息吗,到code选项找到那个参数做的限制。

对于第一个不能为空为;required做的限制。

通过后端打印输出error_messages可以看到是那个参数做的限制。

             error_msg=error.as_json()
print(type(error_msg))
print(error_msg)

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAmgAAAAlCAIAAAB51IKFAAAG0ElEQVR4nO2dzdXrKAyGbx/TgV2Iy6AMqrgs55zZDovpga+s28AslGBZBhkTjO34fY4XSfCPBILXKE74NeaZrA/B22kQn1uf+JAzDMYFZ4bFPsNkfQg/b8JyB+tZkbfLy2WL6lBOOFnv7VTuVCHDuyprzAUAAHAlfunFNOKvRK5GOB3TYF2EjAvBmb1FdYgTNhfOYTAuhOT9BwAAgDuyIZzEYByfI07W84naQO+ZMCQ1xrNPjFuIsXGL2RhXL6WoDv2E/O1kPTdyr3CSZP78/DgDyQQAgO+hSDjXTNazpOusmjwLKnKhi0Oc4/Mw40LyEL2oDv2EUe1EkQvLozCDBACAp1IpnAAAAMAzgXACAAAAO4BwAgC+nz9//cH2qO3QcIJwAgC+n9PHcWydt0PDCcIJAPh+Th/HsXXeDg0nCCcA4PvpM56C04FwAgBAGyCcDwHCCQAAbYBwPgQIJwAAtAHC+RAgnAAA0AYI50OAcAIAQBsgnA8BwgkAAG2AcD4ECCcAALQBwvkQIJwAANAGCOdAiw/vXFH4dmZcRTiNS6zD/F4UrMcqzUdcKy4fllyn7HSXe9Lcr2+tqL3QOnoVy8fS6vF8BdxLQQvz9VxltklE8fH0uBB9LV3sTMnOnbteH+HcjN5NM9Z1qPhFO/NeFhua1rhMXmt91F42hDO3erMLr5WorQ9HN8YR11JWpb6Cyz1p7lfyhBSpubVadT5fvfwsPrF878LpPSHLul2uSYiuhPOovkztVrJnn653Ch9Gr6hD3a9hsp6NJLyhtXF+edReKoXT+kCjnnHh88WldY64VoVw9nS5J839yp1QRGp54EI4r0Zn4WwSonw8PbQvlwtnt67Xn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StHy8QTHL8QfQwjeijvEnBmbtOq9Q/LnKGH+dlAJG+EaP23OL5d/pEhBqV6d+A3NYObfqMWz0RAg/BKPjYh7IFlRuQx/QVM275X//Bd+lg+MCDOqG6VGOIt/kFf4u/hFDOSfRON+ibylc4432eldb8xH1GZsRO/WajqfatlTlOhVivQ61Lm9cN6IVjPOy6IknQD4GsoTA/jnoG/lisI5Zv5G5+5U/HPQjfgwfQ/AXSh/ThXC+ZWsUw6X+OcgcCPYD6FurPoAHAGE8yFc5b9qAQDg7kA4HwKEEwAA2gDhfAgQTgAAaAOE8yFAOAEAoA0QzocA4QQAgDbE8RTbQ7ZDwwnCCQD4fk4fx7F13g4NJwgnAOD7OX0cx9Z5OzSc/gf3D1cEDE9btwAAAABJRU5ErkJggg==" alt="" />

还要其他的form验证比如说IP、mail等等!

 三:form验证扩展:如上是form为我们提供了验证。如果form里没有给我们提供验证呢?我们可以自定义验证扩展。

比如手机验证:

 def mobile_validate(value):
mobile_re = re.compile(r'^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$')
if not mobile_re.match(value):
raise ValidationError('手机号码格式错误')

我们可以在我们form类中定义相应的字段,需要导入模块:from django.core.exceptions import ValidationError用参数:validators=[]后面是列表。

code:

 from django.core.exceptions import ValidationError
import json,re
# Create your views here.
def mobile_validate(value):
mobile_re = re.compile(r'^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$')
if not mobile_re.match(value):
raise ValidationError('手机号码格式错误')
class Loginform(forms.Form):
user=forms.CharField(required=True,error_messages={'required':"用户名不能为空。"})#需要继承forms.Form这个模块
pwd=forms.CharField(required=True,min_length=,
max_length=,
error_messages={'required':'密码不为空!',
'min_length':'不能少于6位!',
'max_length':'长度不能超过10位!'})#require=True表示该字段不能为空。
num=forms.IntegerField(error_messages={'invalid':'请输入数字!','required':'输入不能为空!'})
phone=forms.CharField(validators=[mobile_validate])#注意的是函数名不是字符串。

aaarticlea/png;base64,iVBORw0KGgoAA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obXeWzvXqZvJd3XieYgL6RK75gemnHtb+qRCS/kqWVt7wFwQT0kFx5MNz/K08w/UUlIvmV7KNSWC+TRzABPWQKpqFKRPIr2cdysfyuwkeXl3ZcI5iAvpGrD4Zvq8G0ZfvVp24b+bKxkn3MFMooh7sQTEAPydUHI28w/Xl8CCagh0zB9Mfx2bL9KsEE9I0/mH745pVvxodgAnpIrj0YvaMG09bdN/8wPnxXDughufZw9M5J7Te/Xzv2bNvE/JbtV7tftk3Mv3bsGb/5DfSNJ5jW19dXn7448/nYFlIJ6CG57gsmAOgYwQQgOXLj4egAwQQgJXLj4ejA+wQTgIQQTACSIzcfjt4lmACkhGACkByCCUByZJ5gApAYmX9EMAFIC8EEIDly69HoIMEEICUEE4DkyMKj0SGCCUBKZOHR6NApgglAQmThMcEEIC0EE4DkyMLj0WGCCUBKCCYAySGYACRHPn4yOnyaYAKQELn9ZPRzgglASggmAMkhmAAkR+6sjo4QTABSQjABSI7cJZgAJIZgApCc/wHM/FLq0Snq0gAAAABJRU5ErkJggg==" alt="" />

我们可以定required的error_message来指定我们想要的内容。

code:

 phone=forms.CharField(validators=[mobile_validate,],error_messages={'required':'输入不能为空!'})

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAYYAAAB8CAIAAADinIB5AAAQGElEQVR4nO2d21YiVxrH93vMxTzGXM4jzCPM5TxCd7JmMklnbDOZPmQlBp00OtqdVjGCpj2AB8CYbhXEAydR2+NEUZQWKE49NGs5FyghQH17VwEFFv/fqouiap+q1t6//vZmSzMpk7N7w461XxyrR6OTyx1fGCRJugYAgGbApEzOvhm2r98qqRNKAgA0DXYTJW2cODzHo9Ouji97CCWZADCZtOygoN1gUiY37z1zbIad6yejNk/Ho2e0krRsHGhB6tIHGGMNTa+6WPGKFDWpQe3XJYUo6dzhPXNshs3zGw+f9kFJgKDYBxgPupz6DmluYyqbVPxYel3unPux9kcABZiU+WD3RRy+c6cvYnb4Or8egJIAQamSSq8rHbGK4hGlvuNWwdVT1byE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ov7CQWd1M/Lh988d2IJElyqXt6esIkjLHSk+JHIjGdppSenh4tXw0AQHuYlM07t+M/7UiLb1OvVg7/+d0ooaTu7u4TEsZY6UnxY2Wy0ltlH+Xo7u7W8tUAALTnVkm70s97qVcrh18aKSUZDIZf5CmYpXBS/FikLA2RXQ6DwaDlqwEAaM+NkhZ3k6/30xOuoy+NZlpJ/20eUBIAuodJ2fzCjZIyE+7jfxnHCCV1dXUdNY+uri4tXw0AQHuYlM3/tBP/eS/55iAzsXr8qJejpMPmASUBoHtulPR6L7V0kJ1cPX7URynJYDAcNA9M3ADQPUzK5hdLlPS4b5xQksfj6e7u7moGBoPB4/Fo+WoAANrDkkUlHWaneEoCAICGwpLZ/M+7iTf7qeXD7JTnv0/+AyUBAJpGiZKOoCQAQJO5UdLSfmrlKDsNJQEAmgpLZvOvoSQAQGvAktn867eJpf206yhrhZIAAE2FJd/n37xNLB+kXUdZ69rxk34oCQDQNEqUdJy1rh0/RZQEAGgev4mSbFASAKCpsOT7/NJeYuUg5YaSAADN5lclFdaSnv6H+hs3AABoKAUlSSsHKfdRxgYlAQCaCku9zy/tSa7D1OpxxuY5etJH/YQbAAA0FJZ6n1/el1yFKAlKAgA0FZZ6n1/ek1wHydWjtM1z+KSX+u1tAABoKCz1Pr+yJ7kOku6jtG314InxBygJANAsWOp9fmU/4TpMrkJJAIBm838aqDYvT3TFWQAAAABJRU5ErkJggg==" alt="" />

B:当页面有很多数据需要提交和验证,我们不能写每个input标签,当需要修改的时候,我们不能修改每个input标签,于是有第2种方法,form提交数据。

页面代码:

 <body >
<div>
<form action="/login/" method="post">
<div>{{obj_form.user }}</div>
{% if obj_form.errors %}
<span>{{ obj_form.errors.user.}}</span>
{% endif %}
<div>{{ obj_form.pwd }}</div>
{% if obj_form.errors %}
<span>{{ obj_form.errors.pwd.}}</span>
{% endif %}
<div>{{ obj_form.num }}</div>
{% if obj_form.errors %}
<span>{{ obj_form.errors.num.}}</span>
{% endif %}
<div>{{ obj_form.phone }}</div>
{% if obj_form.errors %}
<span>{{ obj_form.errors.phone.}}</span>
{% endif %}
<div><input type="submit" value="提交" ></div>
</form>
</div>

后台代码:

 def login(request):
if request.method=='POST':
obj_login=Loginform(request.POST)
if obj_login.is_valid():
return redirect('/login/')
else:
error=obj_login.errors
print(error['user'][])
return render(request,'login.html',{'obj_form':obj_login})
obj_form_get=Loginform()
return render(request,'login.html',{'obj_form':obj_form})

总结:

1、form提交验证和ajax提交验证,后台没什么变化,只是前者返回的页面,后者返回的是HttpResponse字符串。

2、form提交的时候,第一次是get请求的,所以第一次的obj_form_get对象内不包含我们提交数据。而POST请求的时候,obj_login对象他包含2部门内容:1:我们在前台表单填写的数据。2:包含我们验证的错误提示。

3、再去错误提示的时候,error默认的数据格式如下:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAgwAAAAuCAIAAADLH9ZMAAAJl0lEQVR4nO2dMXLcOBBFfR4gV2plU6VSJita1VLZbGkDZ6ZOgRso4i04VT6KYt1CG4AEmwC6CfSQsxzrv3IwQwKNZgPoT4Jj4dvHx8fHx4etpO269mDokUPbde3h0Han06lrD9Zac2i7vj+dTq4ZSoYjfe8aM1UPxwN9H9tPkWt5f9puKNC7ZqpoGkcqem9LaNxUyzkXKgoGfUD63jWkjGsMdd610+chdPUGF90OwQnGfUx0nSKEl8OYxnVtfKTv2oMxh7YLznTteGrwJxwcajVu1pvzryVEBv21ZE9R4+GULx+XHL9GU8Mf9zGXuwmAffJNIRLR9JgOusbOp5y11s/1WUky/ylRxUK4WlSurLVtN7lBP/uvJe3O8oVpXN9njacGQ1KW40CTi86ggDduGjdIVNIFtZ0ihJcjjBDh1KJIpNmWE4kgYJPIBSNz/etzuT5ti9ZKYxLGeaxhh7bruq7va5UMgJ2wnkiMc2M/IsG5Ed0gR2lCgOYdWkU2yF0vdTLKLGqDHL7CkK/8p3nOUoiE3MspgkjMymTvP0hA4uE0D12pJ/MniZ4KEtNW9CQxMzh/SGq7KZLm4CXeNK7vK3sNgD2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ucS4Lz8nVpDPIxfHs+Cl3P8fvl+Pny9Hbzffyw3/QRjShzc/f+ornk5Va0Bi85v3RkPZTnFwdXSzeidMlBOb+YtoQMUJhgazlfJGI/ftw/+VT49pwN1rLf6U3EYNNH5PHh8/G7zuHFtujcM7cPUfeEwubm7p1ciHDJNvRcjc+CwdfHaMSUDjvOefmUwiD9mjVYOAwov1+Ovl/eno906McTZt5f0zRLtDk7oqaEPrNZ52r2FlhxydtReJM+ReP5zk7q/vR2Y4RAyTGU+4vzUJ5fHHItbkRJAVElB/kU2xY/iYQMsNEwW18kAjqRyN/aj0cUCbeqrR/3T3R0vj5Og9XcPrzf39CS9KsnOxxfH49pyULkWWFu7t7LbgkF50uu6xyDNIZDgfqh/PZ89D31+niMumzWR3RqzacxrciNKHNz9x4+PN8pXOVu0vcjElWPEeZ2dkMWQi0EajGGXH+VeFglEnItYURx6JLDKvMrnURDsXkG2GiY/QcmOCJcxYiRwgAAAABJRU5ErkJggg==" alt="" />

类似于列表,其中内容部分,是索引为0的字段。所以取值的时候如后台和前台代码那样取值:error['user'][0] 、obj_form.errors.num.0。在js中.点代替python中的[].

4、由于返回页面的时候,默认带有我们添加的<span>标签。所以需要去掉这个标签。

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAecAAABJCAIAAACIDnkpAAAYDUlEQVR4nO2da3BT153Amelkp5MvO8N2uhNnhpROZzbMpNuZhLRNy8526LJQHrNM0w2ENC8IiW0gdTcBbBy3DhjihHccIGAbMH5i+SXLkmVZliUhvy3JwhZ+yZYly3pY77eurnTPfrj0crEeyNhgy/x/8/9gXZ1z7znnWj//de69x6tQUkGECdkd6d0uBUIIc2P8bO6ZF0+yDzZ0nhUrSmUIIUWpLH91LhVayRRZcaRBydhVbp+0kS9ddpeQKXDZnUvVEQAAgMdj1VI3AAAAAJgHYG0AAIBkAqwNAACQTIC1AQAAkgmwNgAAQDIB1gYAAEgmnglrY26M+VGN/Eb/Uz6uZ3aasf/n6o6GxdmbB09L67h1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2uUQJpWDLiLnls6agN4gQMioM3L+ycD++uNYOYf62r94xDXf77bN3vk3HvE56mXAIbz/9gU7G1ytERVt/SOXp85rbDQTCeXnyd98VikQGv/++VaNam5oh0et9aWmSiQn3xx/f0et9eXlysdjY2Wl6Eta2a4dbT7yFeZ2zo33C0x+GggFEECPc66W7UsjOii+kUoUfae2FDBQJlVnHmjCh3qVXQQmLWy6RNd5scNld82oVAMTn/wHt7Ej+12wIdgAAAABJRU5ErkJggg==" alt="" />

所以在前台页面加入if判断,来确定是否需要添加span标签。

         {% if obj_form.errors %}
<span>{{ obj_form.errors.phone.}}</span>
{% endif %}

C:指定生成特定的标签和属性。

form类中添加相应的属性。

 test=forms.CharField(widget=forms.TextInput(attrs={'class':'test'}))

在前端显示相应的标签:

 <div>{{ obj_form.test }}</div>

效果:

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAncAAAAqCAIAAABeJ+o1AAARmklEQVR4nO2daVcbV5rH8xnybk5/hTnzZs6co+POJDPu484kGSehZ3zi05kkjt1xOhvBSbpjYROwzWKwsZoAZhUgMGCzCAkQyEgCJGGEpFJpQStmRyzaxSKB1poXRQqhBQshWQae3/m/wKW6VfXc59b91711Zb3WjToZqi3a6PwvD3s2NjawVxs+n5+ZmZmZmcnn80O3BwIB4YCA2zXk2fEQG5UtqIImI/5WtqChH+39M4iNlwpmuM8DvsBIHmdZagw98rR6ml7ftba0lqKIAAAAgBPMa8fLZQOBQEFBQUFBQSAQCPvIs+MZZQ4LWXyf14dvCbVSBU0W02UxzKRee3Zf4DJv8W489Wzu+fS8Yb6Xxlie2+e7AAAAABAnx8xlMQzTaDQajSbqR36/XzT0bLiHu+PexjBscWy+94tuz4bHKFmq+bfyA1zWs+nh3Xiq7Z7U0tXEximloa+ZaV21pCwUAAAA4IRz/Fz2YIKBICqUqURKDMM8mx7uDfb93xWwvmWMlwpwW1W2oHdfzyO0ODaPF9QzNJ0ftTpm7fg/Nxwbo8zhDcd6ugIBAAAATgAnzWX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AAElFTkSuQmCC" alt="" />

也可以生成自定义的属性。或者select标签等。

 choice=(
(,'大连'),
(,'沈阳'),
)
class Loginform(forms.Form):
user=forms.CharField(required=True,error_messages={'required':"用户名不能为空。"})#需要继承forms.Form这个模块
pwd=forms.CharField(required=True,min_length=,
max_length=,
error_messages={'required':'密码不为空!',
'min_length':'不能少于6位!',
'max_length':'长度不能超过10位!'})#require=True表示该字段不能为空。
num=forms.IntegerField(error_messages={'invalid':'请输入数字!','required':'输入不能为空!'})
phone=forms.CharField(validators=[mobile_validate,],error_messages={'required':'输入不能为空!'})
test=forms.CharField(widget=forms.Select(choices=choice))

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAHYAAAA+CAIAAABxznhHAAAD60lEQVR4nO2ay1baQBjHeZLu3LnzAfICLn0SPcWj4nHfWrW1FXpU7AUfoKXaWmvTi0Kt3G+92BVkFBU3ttUKJ/N1MYBALmRCEiCZ3/kvZDLkDL98fswhcQ0MDv2+hr28SPIyItzxv7i4uAA50gU8Grj0+LOTK+neicefHQ1cpgtYds1dx0WleD2Mp1YzFi9RCx5/NhASu70KeVwDg0N/riGUF0mC7RRPLKcsXqIWJlfSDlLscrk0jivN1AFTLH/IWYrDeTFsvuL6BFl0fgIA6CPF4bwYjAh3TVZMNQ0AOI5rvBIcx0nn9IPigkgSjFIrVqpKlfKsD2oRzfN849l4npfOsbni1jMqf901mqXqEvVCli1h6AvFXwoiyauoMNuBYiLL8E1FvZBlSxiY4paj6iNKcBynVMLQF4r3CyLJRgeKG32ptGB9Owqe55VKGPpAcRn2BZFkIybMrpmrWH1EH45QrEWfoxV/FUSSzZhwj1Kxyr95yy4ClLtEh677QjEm2YwhWsW9AFNsOr2u+G8ZDgR8oE0x+72YlppihElex9CcsmJy12N6LefxZ3sn02u5Xr/r0aQ4juaeKCoGgJGZ3XFfrOs3kxoz7ouNzOxaaY0KasVub/TWAvRaxn0xK61RUVUcQZjkTTvFE8sp9U8LoHVcaaaOTK6krfGlA+sUSw+pzLSh4ijCJFtxNG+a4voEWeyu+AiTbMXR/FNzFRtewvZX3BYVv7JH7ak4doRJ3sbRgjlVTFC6KkzxDeqKAdpYNqldMMVMcRniR5hkuwPFAPJ/Sy+ALHZXfIxJthOmK3ZqFXesGDTo0zJiT8WXZUgcY5J3CXRf16ZNyTs0789UYIqrtN20dSV9oDh5jEl2EugBU2woNcVFnCzWFD9jio2kWXER7ySZYoPRo9jK9WmkDxSniphkJ4kWO1Zs1AMo2rG5Yo3P+LQ8lWL4oyqdvN1UjKliLc8DSh8Lkp5BNz2vuALpE0zyPoUWn+tsFFKbsoMOVZw5wSR8Cj00U7HsHOlLWmyuWHsvhmbFTurFHVextGxVvDuxUVxVIHuKST6k0SO9jaJ6Rqa4GYMVK7Va6SHHKc6dYpKPuhQrbRtArvm2jMu+pMXmiqn2ao0THPR1d1WBb2eY5FMGLQX0NArZzqC9bJniG9jPQLRUFX8/wySfM8jLFBsKU2w6VcU/SphkN4u862qKby8dWLk+jbiXIt1egiLUiofHgm5vZGo10ztxeyPDY0ErrVHhGhgc+leBnyVMspdFPlXFDFqYYtOpKj48xyShHHrMFBsKU2w6VcW/zvFhSTwsiaGswBQbS62KSyKxzBQbzo1iVsUm8R+Xt6pgwYSsZAAAAABJRU5ErkJggg==" alt="" />

the django travel three[form表单验证]的更多相关文章

  1. 第三百一十一节,Django框架,Form表单验证

    第三百一十一节,Django框架,Form表单验证 表单提交 html <!DOCTYPE html> <html lang="en"> <head& ...

  2. Django中的Form表单验证

    回忆一下Form表单验证的逻辑: 前端有若干个input输入框,将用户输入内容,以字典传递给后端. 后端预先存在一个Form表单验证的基类,封装了一个检测用户输入是否全部通过的方法.该方法会先定义好错 ...

  3. Django基础之Form表单验证

    Form表单验证 1.创建Form类(本质就是正则表达式的集合) from django.forms import Form from django.forms import fields from ...

  4. 九 Django框架,Form表单验证

    表单提交 html <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  5. django之form表单验证

    django中的Form一般有两种功能: 输入html 验证用户输入 #!/usr/bin/env python # -*- coding:utf- -*- import re from django ...

  6. Day19 Django之Form表单验证、CSRF、Cookie、Session和Model操作

    一.Form表单验证 用于做用户提交数据的验证1.自定义规则 a.自定义规则(类,字段名==html中的name值)b.数据提交-规则进行匹配代码如下: """day19 ...

  7. django form表单验证

    一. django form表单验证引入 有时时候我们需要使用get,post,put等方式在前台HTML页面提交一些数据到后台处理例 ; <!DOCTYPE html> <html ...

  8. Django(5) session登录注销、csrf及中间件自定义、django Form表单验证(非常好用)

    一.Django中默认支持Session,其内部提供了5种类型的Session供开发者使用: 数据库(默认) 缓存 文件 缓存+数据库 加密cookie 1.数据库Session 1 2 3 4 5 ...

  9. web框架-(六)Django补充---form表单验证

    一.form表单验证 1. 常规html页面的form表单验证 常规页面中,如果想实现对表单中用户输入信息的数据验证,需要配合Ajax来实现. 使用前我们先来熟悉下函数参数:request,其中包含的 ...

随机推荐

  1. CentOS 中 配置 Nginx 支持 https

    一.基础设置: .yum -y update .yum -y install openssl* .cd /usr/local/nginx/conf .mkdir ./ssl .cd ./ssl # 在 ...

  2. 回归JavaScript基础(七)

    主题:引用类型Function的介绍. 今天首先说的就是Function类型.下面就是定义函数的两种方法,第一种使用函数声明语法定义,第二种使用函数表达式定义.这两种定义函数的方式几乎没有什么区别. ...

  3. oracle截取字符串去掉字段末尾指定长度的字符

    lengthb(string)计算string所占的字节长度:返回字符串的长度,单位是字节 length(string)计算string所占的字符长度:返回字符串的长度,单位是字符 eg: //去掉该 ...

  4. 【Oracle】PL/SQL Developer使用技巧(持续更新中)

    1.关键字自动大写 在sql命令窗口中输入SQL语句时,想要关键字自动大写,引人注目该怎么办呢? 一步设置就可以达成了.点击Tools->Preference->Editor,看到截图中这 ...

  5. JAVA使用POI如何导出百万级别数据

    用过POI的人都知道,在POI以前的版本中并不支持大数据量的处理,如果数据量过多还会常报OOM错误,这时候调整JVM的配置参数也不是一个好对策(注:jdk在32位系统中支持的内存不能超过2个G,而在6 ...

  6. [翻译] Icon and Image Sizes

    Icon and Image Sizes iOS Human Interface Guidelines Every app needs an app icon and a launch file or ...

  7. Linux less/more命令详解

    less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻, 只能往后面看,但若使用了 less 时,就可以使用 [pageup] [pagedown] 等按键的功能来 ...

  8. python字典去重脚本

    #!/usr/bin/env python # encoding: utf-8 #字典去重小代码 import sys import os import platform try: pass exce ...

  9. print in或者not in, 判断在不在里面

    print("不疼" in "麻花疼")        # 结果False print("不疼"in "真不疼") # ...

  10. 激活office软件

    1. 打开校园软件正版化激活网址 http://nic.seu.edu.cn/2015/0113/c12333a115290/page.htm 2. 下载KMS激活脚本 3. 登陆easyconnec ...