django-forms表单验证
django生成登录随机图片验证码:http://www.cnblogs.com/wupeiqi/articles/4786251.html
def insert(request): # print("email",request.POST.get("email")) if request.method=="POST": print(time.strftime("%Y-%m-%d %H:%M:%S")) # login_tmp.objects.update(lastTime=datetime.datetime.now()+datetime.timedelta(hours=2)) # login_tmp.objects.update(firstTime=datetime.datetime.now()) rel=BaseResult() obj = MyformsEmail(request.POST) if obj.is_valid(): print(time.strftime("%Y-%m-%d %H:%M:%S"),"forms is ok") isOkData=obj.clean() email =isOkData["emailss"] # 生成6位的0-9的随机数字,并转换成字符串 生成验证码 random_str = "".join([str(random.randint(0, 9)) for i in range(6)]) yanzheng=login_tmp.objects.filter(email=email,tmpType='1') if yanzheng.count(): timenow=datetime.datetime.now() print(timenow, "yanzheng is ok") # login_tmp.object(es.filter(lastTime__gt=F("firstTime")+0.5).update(times=0) # 获取一个当前时间减去一小时的时间,用于判断提交请求的时间间隔是否合法 thisTime = datetime.datetime.now() + datetime.timedelta(hours=-1) print("thistime:",thisTime, "datatime",datetime.datetime.now()) if yanzheng.filter(firstTime__lt=thisTime).count(): print("okokokokokokokokok") try: # objEmail=sendemail.Send_email() # objEmail.send_mail(email,random_str) yanzheng.update(tmp_num=random_str, firstTime=timenow, lastTime=timenow, times='1') rel.supmary="验证码发送成功" rel.code='100' except Exception as e: rel.data = "验证码发送失败" return HttpResponse(json.dumps(rel.__dict__)) if yanzheng.filter(firstTime__gt=thisTime,times__gt=5).count(): rel.error = "1小时内只可以请求5次验证码" rel.status=False print(rel.error) return HttpResponse(json.dumps(rel.__dict__)) else: print(time.strftime("%Y-%m-%d %H:%M:%S"), "yanzheng.filter is ok") try: print(email) print(random_str) # objEmail=sendemail.Send_email() # objEmail.send_mail(email,random_str) yanzheng.update(times=F("times") + 1, tmp_num=random_str,lastTime=datetime.datetime.now()) rel.data='验证码发送成功' print(rel.data) except Exception as e: rel.data = e else: try: login_tmp.objects.create(email=email,tmp_num=random_str,times=1,tmpType=1) except Exception as e: rel.data="验证码发送失败" return HttpResponse(json.dumps(rel.__dict__)) else: # rel_myform=obj.errors.as_json() # rel_myform=obj.errors.as_json() rel_myform=obj.errors['emailss'][0] print(rel_myform) rel.status=False # rel.error=rel_myform['emailss'][0]['message'] rel.error=rel_myform print("rel_myform:",rel_myform) print(rel) return HttpResponse(json.dumps(rel.__dict__))
HTML
<div class="login-input" style="position: relative; "> <span class="insert_err hide" id="insert_err" style="font-size: 10px;color: red">报错--</span><br> <select style="width: 205px;height: 30px;border: 1px solid #CDDDEF;padding: 5px 5px;"> <option>中国(+86)</option> <option>中国香港(+852)</option> <option>中国澳门(+853)</option> <option>中国台湾(+886)</option> </select><br><br> <input style="width: 115px;height: 20px" type="email" name="emaisl" id="emails" placeholder="邮箱"> <button id="emailBt">获取验证码</button><br><br> {# <--#} {# <input style="width: 115px;height: 20px" type="text" name="phonenumber" placeholder="手机号">#} {# <button id="phoneBt">获取验证码</button>#} {# <br>#} {# <span style="font-size: 10px;margin-left: 40px">收不到短信? <a href="javascript:;">获取语音验证码</a></span>-->#} {# <br><br>#} <input style="width: 200px;height: 20px" type="text" name="code" placeholder="验证码" id="tmpNum"><br><br> <input style="width: 200px;height: 20px" type="password" name="password" placeholder="密码" id="passwd"><br><br> <button class="loginNext" id="loginNext"> 下一步</button> </div>
JavaScript OR jQuery
function insert() { $("#emailBt").click(function () { //alert($("#email").val()); //console.log($("#emails").val()); $.ajax({ url:"/insert/", data:{emailss:$("#emails").val()}, //dataType:JSON, type:"POST", success:function (rel) { console.log(rel); var rel_data=JSON.parse(rel); if (rel_data.status){ console.log(rel.error); console.log(rel_data.error); $("#emails").attr("disabled","disabled"); $("#insert_err").html('验证码发送成功'); alert("验证码发送成功"); }else{ //var err=JSON.parse(rel_data.error); var err_msg=rel_data.error; console.log(err_msg); $("#insert_err").attr("color","red").removeClass("hide").html(err_msg); } } }) });