django-simple-captcha refresh & validation

原创 2015年07月06日 18:40:34

Preparation

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

Add captcha to the INSTALLED_APPS in your settings.py

Run python manage.py syncdb (or python manage.py migrate if you are managing database migrations via South) to create the required database tables
Add an entry to your urls.py:

urlpatterns += patterns('',
    url(r'^captcha/', include('captcha.urls')),
)


Django-simple-captcha 0.4.3 and later supports both Django 1.7’s new migrations and South migrations: if you are using South and Django < 1.7, you must define the following in your settings:


SOUTH_MIGRATION_MODULES = {
'captcha': 'captcha.south_migrations',
}
</pre><pre name="code" class="python"><strong><a target=_blank href="https://docs.djangoproject.com/en/1.8/intro/tutorial01">Create a app</a></strong>
</pre><pre name="code" class="python">python manage.py startapp ***
</pre><pre name="code" class="python">Add a form
***/forms.py
</pre><pre name="code" class="python">from django import forms
from captcha.fields import CaptchaField

class NameForm(forms.Form):
    captcha = CaptchaField()


Add urls
***/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'),
)
Add views function
***/views.py
from django.shortcuts import render
from django.http import HttpResponseRedirect

from .forms import NameForm

def get_name(request):
    if request.method == 'POST':
        form = NameForm(request.POST)
        if form.is_valid():
            human = True
            return HttpResponseRedirect(request.path + '?ok')
    else:
        form = NameForm()
    return render(request, 'polls/name.html',{'form':form})

Add Template file

***/templates/***/name.html
<form action="." method="post">
    {% csrf_token %}

    {{form.captcha.errors}}
    {{form.captcha}}
    <!--{{ form }} -->
    <input type="submit" value="Submit" />
</form>

<script src="http://libs.baidu.com/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
    $('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;
    });
});
</script>



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

相关文章推荐

django-simple-captcha & jquery validate

1. view.py from django.shortcuts import render from django.http import HttpResponseRedirect, HttpRe...

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 - 验证码 django-simple-captcha 使用简介

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

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...

基于 Django1.10 文档的深入学习(24)—— Form and field validation 之 cleaned_data

Form field default cleaning表单域默认清理我们首先创建一个自定义表单字段,验证其输入是一个包含逗号分隔的电子邮件地址的字符串。全班看起来像这样:from django imp...

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...

在FormWizard中使用Django-sample-captcha一直出现"invalid captcha“错误

如题,在github上有作者对这个问题的回答,地址,不过略坑的是~问题是在两年前就提出来,然后作者最近才回答,无语了 FormWizard验证流程大概是下面这样子的: 处理post,验证  ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

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