django 添加验证码2

部署环境:

Centos 6.5  /   python2.6   /    django1.6

1、安装 django-simple-captcha

# pip install django-simple-captcha==0.4.6

安装Pillow库(Pillow-2.6.1),版本不对会生成不了图片报错:

ImportError: The _imagingft C module is not installed

想测试能不能生成图片可以到我上一篇文章调用生成验证码图片脚本

2、添加captcha到项目的setting.py文件INSTALLED_APPS


CAPTCHA_IMAGE_SIZE = (80, 33)


CAPTCHA_FOREGROUND_COLOR = '#001100'


CAPTCHA_NOISE_FUNCTIONS = ('captcha.helpers.noise_dots',)


CAPTCHA_TEST_MODE = False


3、同步数据库结构


django1.6版本执行 python manage.py syncdb


django1.6以后的版本执行 python manage.py migrate


4、添加到项目url.py


urlpatterns = [


....


    url(r'^captcha/', include('captcha.urls')),


]


5、forms.py


from django import forms


from captcha.fields import CaptchaField


class CaptchaTestModelForm(forms.Form):


    captcha = CaptchaField()


6、添加到视图view.py(具体根据自己实际情况配置)


import forms


def some_view(request):


    if request.POST:


        form = forms.CaptchaTestForm(request.POST)


        # Validate the form: the captcha field will automatically


        # check the input


        if form.is_valid():


            human = True


    else:


        form = forms.CaptchaTestModelForm()


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


7、添加到template.html

{{ form.captcha }}

8、点击刷新

关联一个JQ文件,然后添加代码

$('.captcha').click(function(){

        console.log('click');

        $.getJSON("/captcha/refresh/",

                  function(result){

            $('.captcha').attr('src', result['image_url']);

            $('#id_captcha_0').val(result['key'])

          });

    });

官方参考文章:http://django-simple-captcha.readthedocs.io/en/latest/usage.html#installation

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 版权: https://github.com/haiiiiiyun/awesome-django-cn Aweso...
    若与阅读 23,354评论 3 241
  • # Python 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome - XXX 系列...
    aimaile阅读 26,682评论 6 427
  • 模板标签除了几个常用的,还真心没有仔细了解一下,看到2.0发布后,翻译学习一下。 本文尽量忠实原著,毕竟大神的东西...
    海明_fd17阅读 6,148评论 0 5
  • 上周我们看了恋爱主题的《卡罗尔》,谈到了二位女主面对的社会现实,本周我们看了一部更加现实的婚姻主题电影《革命之路》...
    幺幺是我阅读 3,273评论 2 0
  • 我是一把剑,一把有故事的剑。 1 打造我的铸剑师是一个疯老头,整天神神叨叨的。 我出生的那天晚上风雨交加,电闪雷鸣...
    闲猫吃鱼阅读 3,671评论 0 1