django-simple-captcha & jquery validate

原创 2015年07月07日 16:35:27

1. view.py

from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse
import json

from django import forms
from captcha.fields import CaptchaField


class NameForm(forms.Form):
    captcha = CaptchaField()
# Create your views here.

def get_name(request):
    form = NameForm()
    return render(request, 'polls/name.html',{'form':form})

def verifyCaptcha(request):
    result = 'true'
    _captcha_0 = request.GET.get('captcha_0') or ''
    _captcha_1 = request.GET.get('captcha_1') or ''

    form = NameForm({'captcha_0':_captcha_0,'captcha_1':_captcha_1})

    if form.is_valid():
        result = 'true'
    else:
        result = 'false'
    return HttpResponse(json.dumps([result]), content_type='application/json')

2. urls.py

from django.conf.urls import patterns, url
from polls import views

urlpatterns = patterns('',
    # url(r'^$',view.index, name='index'),
    url(r'^$', views.get_name, name='getname'),
    url(r'^verifyCaptcha/$',views.verifyCaptcha, name='verifyCaptcha'),
)


3. templates/app/name.html

<form action="." method="post" id="login-form">
    {% csrf_token %}

    {{form.captcha.errors}}
    {{form.captcha}}

    <input type="submit" value="Submit" />
</form>

<script src="{{ STATIC_URL }}js/jQuery-2.1.3.min.js"></script>
<script src="{{ STATIC_URL }}js/jquery.validate-min.js"></script>
<script type="text/javascript">
$(function(){
    // refresh
    $('img.captcha').click(function(){
            var $form = $(this).parents('form');
            var url = location.protocol + "//" + window.location.hostname + ":" + location.port + "/captcha/refresh/";
            $.getJSON(url,{},function(json){
                $form.find('input[name="captcha_0"]').val(json.key);
                $form.find('img.captcha').attr('src',json.image_url);
            });
            return false;
    });
    // check
    $("#login-form").validate({
                //onfocusout:true,
                //onsubmit:false,
                onkeyup: false,
                rules:{
                    captcha_1: {
                        // remote: '/polls/verifyCaptcha'
                        required: true,
                        remote:{
                            type:"get",
                            dataType:"json",
                            url:"/polls/verifyCaptcha/",
                            data:{
                                captcha_0:function(){return $("#id_captcha_0").val();}
                            } 
                        }
                    },
                },
                messages:{
                    captcha_1:{
                        required:"Verification code required",
                        remote:"Verification code error",
                    }
                },
                invalidHandler: function(form, validator) {
                    //location.reload();
                }
    });
});
</script>

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

django-simple-captcha 验证码插件 和自己实现验证码的

第三方验证码插件的使用官方文档:http://django-simple-captcha.readthedocs.io/en/latest/usage.html#installationgithub:...

django-simple-captcha 使用 以及添加动态ajax刷新验证

django-simple-captcha 使用以及添加动态ajax刷新、验证django-simple-captcha是常用的添加验证码的package,但是使用起来会遇到一些问题,比如ajax动态...

django-simple-captcha refresh & validation

Preparation Download django-simple-captcha using pip by running: pip install django-simple-captcha ...

django - 验证码 django-simple-captcha 使用简介

用了一段时间django-simple-captcha,谈谈体会: 一句话:不好用, 但实在没办法工作中就是要用它,而且发现有些网站也是用的它, 深入学习了下, 发现不是人家不好用而是自己功力不够,还...
  • xxm524
  • xxm524
  • 2015-09-11 12:09
  • 3731

jcaptcha组件小小改造解决Invalid ID, could not validate unexisting or already validated captcha

jcaptcha是一款很好的java实现的验证码组件,官方地址:http://jcaptcha.sourceforge.net/ 在使用jcaptcha过程中,我相信很多人在用ajax校验验证码...

simple_captcha

simple_captcha 博客分类: ROR RailsActiveRecordGitCSSRuby https://github.com/kares/simple_captch...

Django个人博客开发(七:CommentStore CommentShow.、八:blogDetailShow captcha.js、九:search css)

七:CommentStore CommentShow. A:add Comment to lblog/models.py in order to store; B:add lblog/includes...

HOWTO django + celery + rabbitmq simple setup for testing purpose

This setup is simply used for testing purpose and using the official First Steps with Celery and Fir...

jquery.validate.js

  • 2017-10-11 13:58
  • 38KB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)