当前位置:   article > 正文

Django-simple-captcha 验证码插件_django使用simpleui的登陆界面如何增加短信验证

django使用simpleui的登陆界面如何增加短信验证

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

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

安装部署

pip install  django-simple-captcha

settings.py配置,加入captcha

  1. INSTALLED_APPS = [
  2. 'captcha',
  3. ]

配置验证码模式settings.py

  1. # django_simple_captcha 验证码配置其他配置项查看文档
  2. # 默认格式
  3. CAPTCHA_OUTPUT_FORMAT = '%(image)s %(text_field)s %(hidden_field)s '
  4. CAPTCHA_NOISE_FUNCTIONS = ('captcha.helpers.noise_null', # 没有样式
  5. # 'captcha.helpers.noise_arcs', # 线
  6. # 'captcha.helpers.noise_dots', # 点
  7. )
  8. # 图片中的文字为随机英文字母,如 mdsh
  9. # CAPTCHA_CHALLENGE_FUNCT = 'captcha.helpers.random_char_challenge'
  10. # 图片中的文字为数字表达式,如2+2=
  11. CAPTCHA_CHALLENGE_FUNCT = 'captcha.helpers.math_challenge'
  12. # 超时(minutes)
  13. 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 }}

模板中引用

  1. html 模板中显示验证码
  2. <div class="field">
  3. <div class="ui left img input">
  4. <button id='js-captcha-refresh' class='ui icon button ' ><i class="refresh icon green"></i></button>
  5. <img src="{{ image_url}}" alt="captcha" class="captcha">
  6. <input autocomplete="off" id="id_captcha_1" name="captcha_1" type="text" placeholder="输入验证码">
  7. <input id="id_reg_captcha_0" name="captcha_0" type="hidden" value="{{ hashkey }}">
  8. </div>
  9. </div>

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

  1. $(function(){
  2. $('.captcha').css({
  3. 'cursor': 'pointer'
  4. });
  5. /*# ajax 刷新*/
  6. $('.captcha').click(function(){
  7. console.log('click');
  8. $.getJSON("/captcha/refresh/",function(result){
  9. $('.captcha').attr('src', result['image_url']);
  10. $('#id_captcha_0').val(result['key'])
  11. });
  12. });
  13. })

From加验证码功能

  1. from captcha.fields import CaptchaField
  2. class PostForm(forms.ModelForm):
  3. captcha = CaptchaField() #CaptchaField 字段
  4. class Meta:
  5. model = models.Post
  6. fields = ['mood', 'nickname', 'message', 'del_pass']
  7. def __init__(self, *args, **kwargs):
  8. super(PostForm, self).__init__(*args, **kwargs)
  9. self.fields['mood'].label = '现在的心情'
  10. self.fields['nickname'].label = '您的昵称'
  11. self.fields['message'].label = '心情留言'
  12. self.fields['del_pass'].label = '设置密码'
  13. self.fields['captcha'].label = '请输入验证码'

 

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/喵喵爱编程/article/detail/983039
推荐阅读
相关标签
  

闽ICP备14008679号