• django-simple-captcha 验证码插件


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

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

    安装部署

    版本:django 1.9 

    pip install  django-simple-captcha==0.4.6
    
    • settings.py配置,加入captcha
    INSTALLED_APPS = [
        'django.contrib.admin',
        'django.contrib.auth',
        'django.contrib.contenttypes',
        'django.contrib.sessions',
        'django.contrib.messages',
        'django.contrib.staticfiles',
        'captcha',
    ]
    
    • urls.py配置

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

    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')),
    ]
    
    • 数据库同步
    makemigrations
    migrate
    

    应用场景

    • form定义

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

    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代码
    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页面引用
    {{ register_form.captcha }}
    
  • 相关阅读:
    Shell 中的 expect 命令
    #每日日报#7.11
    #每日日报#7.10
    #每日日报#7.09
    #每日日报#7月8日
    每日日报2020.7.17
    每日日报2020.7.16
    每日日报2020.7.15
    每日日报2020.7.14
    每日日报2020.7.13
  • 原文地址:https://www.cnblogs.com/shhnwangjian/p/6376819.html
Copyright © 2020-2023  润新知