django中验证码——django-simple-captcha

转自:http://www.kaixinhaha.com/?p=94

django-simple-captcha 一个很简单的django验证组件,实现原理是数据库中生成key和随机字符,然后将key与sn存入数据库,用户提交时根据key去数据库中查询比对是否正确,没有使用常用的cookies session, 也挺简单,每个验证码都有过期时间,程序会自动清除过期的验证码。

官方文档:https://django-simple-captcha.readthedocs.org/en/latest/usage.html

简单记录一下

1. pip install django-simple-captcha 目前安装的是0.3.4版本。 手工下载可以安装 0.3.6最新版。

2. Add captcha to the INSTALLED_APPS in your settings.py

2. Run python manage.py syncdb (or python manage.py migrate if you are managing databae migrations via South) to create the required database tables

3. Add an entry to your urls.py:

urlpatterns += patterns(”,
url(r’^captcha/’, include(‘captcha.urls’)),
)

4. from中使用

1
2
3
4
5
6
from django  import forms
from captcha. fields  import CaptchaField

class CaptchaTestForm (forms. Form ):
    myfield  = AnyOtherField ( )
    captcha  = CaptchaField ( )

5. views.py human = True 最重要

1
2
3
4
5
6
7
8
9
10
11
12
def some_view (request ):
     if request. POST:
        form  = CaptchaTestForm (request. POST )

         # Validate the form: the captcha field will automatically
         # check the input
         if form. is_valid ( ):
            human  =  True
     else:
        form  = CaptchaTestForm ( )

     return render_to_response ( 'template.html' , locals ( ) )

6. 想增加一个简单的刷新验证码的功能,在当前view增加一段代码,需要获得新sn的时候,返回新sn的图片地址

1
2
3
4
5
     #刷新验证码
     if request. GET. get ( 'newsn' ) == '1':
        csn =CaptchaStore. generate_key ( )
        cimageurl = captcha_image_url (csn )
         return HttpResponse (cimageurl )

前台html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
     <p>
         <label>验证(必填) </label>
         <br />
      {{ form.captcha }}
       <button id='js-captcha-refresh' type="button">刷新验证码 </button>
        </p>
       
<script>
    $('#js-captcha-refresh').click(function(){
         $.get("?newsn=1", function(result){
            alert(result);
             $('.captcha').attr("src",result);
             $('#id_captcha_0').attr("value",result.split('/')[3]);
          });
    return false;
});
</script>

本地添加:

上面的设置是点击一个按钮实现刷新验证码,下面我改成了点击验证码图片本身刷新验证码,实现原理是一致的:

<tr>
                      <th width="121" height="40" align="right"><span> *</span> 验 证 码:</th>
                      <td width="242" height="40" align="left">{{ form.captcha }}
                        <script type="text/javascript">
							$('.captcha').click(function(){ //点击图片刷新
								var tmp = Math.random().toString();
						    	$.get("?newsn=1&tmp="+tmp, function(result){
						        	$('.captcha').attr("src", result);
						        	$('#id_captcha_0').attr("value", result.split('/')[3]);
						        });
						    return false;
						});
						</script>
					  </td>
                      <td width="237" height="40" class="span-not"><span id="capCode"> </span></td>
                    </tr>

还有一个很灵活的配置 CAPTCHA_CHALLENGE_FUNCT = ‘captcha.helpers.word_challenge’ ,自带的有 随机字符 加减算法 输入图中字符的三种验证图的形式,也可以根据自己的需要来定制验证码内容.

配置文档地址:https://django-simple-captcha.readthedocs.org/en/latest/advanced.html

1
2
3
4
5
6
7
import  random

def random_digit_challenge ( ):
    ret  = u ''
     for i  in  range ( 6 ):
        ret + =  str ( random. randint ( 0 , 9 ) )
     return ret , ret


参考链接:

http://newliu.com/post/5/

http://bbs.scoke.org/thread-128-1-1.html


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值