Django-simple-captcha 验证码插件

官方文档:Using django-simple-captcha — Django Simple Captcha 0.5.15 documentation

github:https://github.com/mbi/django-simple-captcha

安装部署

pip install  django-simple-captcha

settings.py配置,加入captcha

INSTALLED_APPS = [
    'captcha',
]

配置验证码模式settings.py

# django_simple_captcha 验证码配置其他配置项查看文档
# 默认格式
CAPTCHA_OUTPUT_FORMAT = '%(image)s %(text_field)s %(hidden_field)s '
CAPTCHA_NOISE_FUNCTIONS = ('captcha.helpers.noise_null', # 没有样式
    # 'captcha.helpers.noise_arcs', # 线
    # 'captcha.helpers.noise_dots', # 点
)
# 图片中的文字为随机英文字母,如 mdsh
# CAPTCHA_CHALLENGE_FUNCT = 'captcha.helpers.random_char_challenge' 
 # 图片中的文字为数字表达式,如2+2=
CAPTCHA_CHALLENGE_FUNCT = 'captcha.helpers.math_challenge'   
# 超时(minutes)
CAPTCHA_TIMEOUT = 1 

urls.py配置

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

1

2

3

4

5

6

7

from django.conf.urls import url, include

from django.contrib import admin

urlpatterns = [

    url(r'^admin/', admin.site.urls),

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

]

  • 数据库同步

1

2

makemigrations

migrate

应用场景

  • form定义

app下自定义froms.py文件,创建一个注册form

1

2

3

4

5

6

7

from django import forms

from captcha.fields import CaptchaField

class RegisterForm(forms.Form):

    email = forms.EmailField(required=True)

    password = forms.CharField(required=True, min_length=5)

    captcha = CaptchaField(error_messages={"invalid": u"验证码错误"})

  • views.py代码

1

2

3

4

5

6

7

from django.views.generic.base import View

class RegisterView(View):

    def get(self, request):

        register_form = RegisterForm()

        return render(request, "register.html", {"register_form": register_form})

  • html页面引用

1

{{ register_form.captcha }}

模板中引用

html 模板中显示验证码

        <div class="field">
          <div class="ui left img input">
            <button  id='js-captcha-refresh'  class='ui icon button ' ><i class="refresh icon green"></i></button>
              <img src="{{ image_url}}" alt="captcha" class="captcha">
              <input autocomplete="off" id="id_captcha_1" name="captcha_1" type="text" placeholder="输入验证码">
              <input id="id_reg_captcha_0" name="captcha_0" type="hidden" value="{{ hashkey }}">
          </div>
        </div>

js刷新验证码,要先进入jquery

$(function(){
        $('.captcha').css({
        'cursor': 'pointer'
    });
    /*# ajax 刷新*/
        $('.captcha').click(function(){
            console.log('click');
            $.getJSON("/captcha/refresh/",function(result){
                $('.captcha').attr('src', result['image_url']);
                $('#id_captcha_0').val(result['key'])
            });
        });
    })

From加验证码功能

from captcha.fields import CaptchaField

class PostForm(forms.ModelForm):
    captcha = CaptchaField() #CaptchaField 字段
    class Meta:
        model = models.Post
        fields = ['mood', 'nickname', 'message', 'del_pass']

    def __init__(self, *args, **kwargs):
        super(PostForm, self).__init__(*args, **kwargs)
        self.fields['mood'].label = '现在的心情'
        self.fields['nickname'].label = '您的昵称'
        self.fields['message'].label = '心情留言'
        self.fields['del_pass'].label = '设置密码'
        self.fields['captcha'].label = '请输入验证码'

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

菲宇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值