Package: django_click_captcha
简介点击倒字验证码，仿照知乎验证码效果实现， 用户通过点击验证码上面的倒着的汉字， 从而通过验证码验证。环境条件Python>=3.4Django>=1.9安装Using pip:$ pip install django_click_captchaor from source code:$ pip install -e git+https://github.com/malongge/click_captcha.git#egg=click_captcha将 click_captcha 加到 INSTALL_APPS 列表当中使用方法配置:CLICK_CAPTCHA_WIDGET_TEMPLATE: 验证码模板代码， 默认为click_captcha/captcha.htmlCLICK_CAPTCHA_TIMEOUT: 验证码过期时间， 默认为 60 秒CACHE_BACKEND: 验证码缓存服务， 默认为数据库:django.core.cache.backends.db.DatabaseCache因此如果使用默认的缓存服务，需要执行下面的命令python manage.py createcachetable创建缓存表urls.py 添加获得验证码的地址:from django.conf.urls import url

from click_captcha import views

urlpatterns = [
    url(r'click_captcha/(?P<uuid>\w+)/$', views.CaptchaView.as_view(), name='click_captcha'),
]加载样式标签:{ % load click_captcha_tags % }
...
{ % include_click_captcha_css  False % }
...
{ % include_click_captcha_js False % }如果希望用安装包自带的 bootstrap 和 jquery 去掉 False 参数添加 form 表单， 比如 admin 登录配置:from django.contrib.admin.forms import (
AdminAuthenticationForm as _AdminAuthenticationForm
)
from django.forms import ValidationError

from click_captcha.fields import CaptchaField


class AuthenticationForm(_AdminAuthenticationForm):
    field_order = ['captcha', 'username', 'password']

    captcha = CaptchaField()

    def _clean_fields(self):
        for name, field in self.fields.items():
            value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
            try:
                value = field.clean(value)
                self.cleaned_data[name] = value
                if hasattr(self, 'clean_%s' % name):
                    value = getattr(self, 'clean_%s' % name)()
                    self.cleaned_data[name] = value
            except ValidationError as e:
                self.add_error(name, e)
                return这里 _clean_fields 是想先进行验证码校验， 如果验证码校验不成功就不进行用户和密码的验证码了新建个 sites.py, 替换原来的 login form:from django.contrib.admin.sites import AdminSite as _AdminSite

from apps.authentication.forms import AuthenticationForm
from apps.authentication.models import User

class MyAdminSite(_AdminSite):
    login_form = AuthenticationForm
    login_template = 'click_captcha/login.html'


admin_site = MyAdminSite(name='myadmin')
admin_site.register(User)在 login.html 中加上验证码 form 字段:{ % load click_captcha_tags %}

{ % block extrastyle %}
    { % include_click_captcha_css  False %}
{ % endblock %}

... ...
{ # form 表单下插入 #}
   <div class="{ % if form.captcha.errors %} error{ % endif %}">
        {{ form.captcha }}
   </div>
... ...
{ % block extrajs %}
  { % include_click_captcha_js False %}
{ % endblock %}效果图备注信息该项目不支持 python 2 版本， django 版本必需高于 django 1.9该项目有一系列文章的介绍其是如何开发的过程， 可以参考文章来定制自己的验证码博客地址History0.1 (2017-08-18)First release on PyPI.CreditsDevelopment Lead码龙哥 -https://github.com/malonggeContributorsLicenseSource code can be found atGithub.The MIT License (MIT):Copyright (c) 2017, 码龙哥

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.