Django Admin框架加上DjangoCaptcha验证码
来源:互联网 发布:瓷砖效果图软件app 编辑:程序博客网 时间:2024/05/21 09:38
Django Admin框架加上DjangoCaptcha验证码
简介:本文是自己工作的总结,用Django自带的Admin框架作为后台,但是缺少验证码,故用DjangoCaptcha 给admin加上验证码。
<1>需要的东西:
DjangoCaptcha
安装:pip install DjangoCaptcha
<2>python后台代码
视图函数views.py
视图函数views.py#返回验证码图片from django.shortcuts import renderfrom django.http import HttpResponsefrom django.http import HttpResponseRedirectimport re, jsonfrom DjangoCaptcha import Captchadef code(request): ca = Captcha(request) #ca.words = ['hello','world','helloworld'] ca.img_width = 150 ca.img_height = 30 #ca.type = 'number' ca.type = 'word' return ca.display()#验证,提交的验证码是否正确def verifyAjax(request): result = 'false' _code = request.GET.get('code') or '' if not _code: return HttpResponse(json.dumps([result]), content_type='application/json') ca = Captcha(request) if ca.check(_code): result = 'true' else: result = 'false' return HttpResponse(json.dumps([result]), content_type='application/json')
<3>html显示验证码
<a href="#" id="get_code_img"><img src="/accounts/verify/code" class="getcode" /></a><input type = "text", id = "captcha_input", name = "captcha_input">
<4>js 代码表单验证
这里我们使用jQuery 的validate 框架的异步验证
//验证表单$("#login-form").validate({rules:{captcha_input:{required:true,remote:{url:'/accounts/verify/verifyAjax',type:'get',dataType:'json',data:{code:function(){return $("#captcha_input").val();},},}}}, messages:{captcha_input:{required:"Verification code required", remote:"Verification code error",} },//未验证时回调invalidHandler: function(form, validator) {//location.reload();}});//刷新验证码$("#get_code_img").click(function() {$(".getcode").attr("src", "/accounts/verify/code?rand=" + Math.random());return false;});$(".getcode").attr("src", "/accounts/verify/code?rand=" + Math.random());
0 0
- Django Admin框架加上DjangoCaptcha验证码
- django admin ModelForm field 验证
- Django框架全面讲解 -- admin
- django 购物系统 - admin注册和验证
- [django]admin后台采用框架页面不行
- Django框架之 Admin(后台管理)
- django验证码框架(django-simple-captcha)
- Django Admin
- django admin
- django admin
- Django Admin
- Django - Admin
- Django-admin
- Django Admin
- Django Admin
- Django admin
- Django-admin
- 给django admin加上在线编辑运行python代码的功能
- HTTP协议详解(真的很经典)
- hdu1272 小希的迷宫
- polymer学习过程中的事
- 校园视频信息管理系统-项目收获
- eclipse快捷键
- Django Admin框架加上DjangoCaptcha验证码
- lua 中pairs 和 ipairs区别
- abcd
- static作用于全局变量、局部变量及顶层函数的作用
- OC总结——Protocol协议
- Using the Razor Syntax
- Xcode 的正确打开方式——Debugging
- 九度OJ 1170 找最小数
- GridView显示数据