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='')
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='')
rel.supmary="验证码发送成功"
rel.code=''
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); } } }) });
django-forms表单验证的更多相关文章
- python运维开发(十九)----Django后台表单验证、session、cookie、model操作
内容目录: Django后台表单验证 CSRF加密传输 session.cookie model数据库操作 Django后台Form表单验证 Django中Form一般有2种功能: 1.用于做用户提交 ...
- django form表单验证
一. django form表单验证引入 有时时候我们需要使用get,post,put等方式在前台HTML页面提交一些数据到后台处理例 ; <!DOCTYPE html> <html ...
- django from表单验证
django from表单验证 实现:表单验证 工程示例: urls.py 1 2 3 4 5 6 7 8 9 from django.conf.urls import url from djan ...
- Django(5) session登录注销、csrf及中间件自定义、django Form表单验证(非常好用)
一.Django中默认支持Session,其内部提供了5种类型的Session供开发者使用: 数据库(默认) 缓存 文件 缓存+数据库 加密cookie 1.数据库Session 1 2 3 4 5 ...
- Django实现表单验证、CSRF、cookie和session、缓存、数据库多表操作(双下划綫)
通常验证用户输入是否合法的话,是前端js和后端共同验证的,这是因为前端js是可以被禁用的,假如被禁用了,那就没法用js实现验证合法与否了,也就是即使用户输入的不合法,但是也没提示,用户也不知道怎么输入 ...
- django book表单验证学习
django提供了强大的表单验证功能form 个人认为是把html代码封装到form里面,就像封装models中的数据一样.而且带有强大的验证功能,不需要后台再去写一堆乱七八糟的验证 具体操作查看dj ...
- Django之表单验证
对于前端的表单进行验证的方法,从最简单的js到基于XML传输的Ajax,再到cookie的免认证,现在Django为我们提供了自带的表单验证方法. views.py: from django impo ...
- Django forms表单 select下拉框的传值
今儿继续做项目,学习了Django的forms生成前端的代码. forms.py class SignupForm(forms.Form): username = forms.CharField(va ...
- Django-website 程序案例系列-17 forms表单验证的字段解释
1.Django内置字段如下: Field required=True, 是否允许为空 widget=None, HTML插件 label=None, 用于生成Label标签或显示内容 initial ...
- Django ModelForm表单验证
ModelForm 在使用Model和Form时,都需要对字段进行定义并指定类型,通过ModelForm则可以省去From中字段的定义 应用场景:定制model admin 的时候可以使用.适用于小业 ...
随机推荐
- android CheckBox控件的定义及事件监听
http://www.beijibear.com/index.php?aid=336 android CheckBox控件的定义及事件监听,本例实现CheckBox控件的定义及点击事件的监听并显示结果 ...
- Ansible@一个高效的配置管理工具--Ansible configure management--翻译(五)
无书面许可请勿转载 高级Playbook Extra variables You may have seen in our template example in the previous chapt ...
- px值转rem值的Sublime Text 3自己主动完毕插件
一个CSS的px值转rem值的Sublime Text 3自己主动完毕插件. 插件效果例如以下: 安装 克隆项目 https://github.com/hyb628/cssrem.git 进入pa ...
- java 接口实现多态
package unit4; import java.awt.Graphics; public interface Shape { void drowme(Graphics g); double ar ...
- 机器学习7—AdaBoost学习笔记
Adaboost算法原理分析和实例+代码(简明易懂)(转载) [尊重原创,转载请注明出处] http://blog.csdn.net/guyuealian/article/details/709953 ...
- LVS+Keepalived(DR模式)学习笔记
1.简述 在互联网的中型项目中,单服务器往往已经无法满足业务本身的性能要求,这时候就会平行扩展,把负载分摊到数台服务器上(集群).一般实现集群有DNS轮询,LVS,nginx负载均衡. 集群主要目的包 ...
- Exception in thread "main" java.util.ConcurrentModificationException
package test; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public c ...
- Swift_4_闭包(Blocks)
import Foundation println("Hello, World!") var arr = [1,2,4,6,74,2] func hasClosure(list:[ ...
- 【BZOJ4785】[Zjoi2017]树状数组 树套树(二维线段树)
[BZOJ4785][Zjoi2017]树状数组 Description 漆黑的晚上,九条可怜躺在床上辗转反侧.难以入眠的她想起了若干年前她的一次悲惨的OI 比赛经历.那是一道基础的树状数组题.给出一 ...
- Hadoop DataNode 节点的动态添加和动态删除
动态添加 DataNode 节点 hadoop环境是必须的 需要加入新的 DataNode 节点,前提是已经配置好 SSH 无密登录:直接复制已有DataNode中.ssh目录中的authorized ...