Django 点击倒字验证码

django_click_captcha的Python项目详细描述


简介

点击倒字验证码,仿照知乎验证码效果实现, 用户通过点击验证码上面的倒着的汉字, 从而通过验证码验证。

环境条件

  • Python>=3.4
  • Django>=1.9

安装

Using pip:

$ pip install django_click_captcha

or 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.html

CLICK_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 %}

效果图

https://malongge.github.io/assets/django/captcha-django.gif

备注信息

该项目不支持 python 2 版本, django 版本必需高于 django 1.9

该项目有一系列文章的介绍其是如何开发的过程, 可以参考文章来定制自己的验证码

博客地址

History

0.1 (2017-08-18)

  • First release on PyPI.

Credits

Development Lead

Contributors

License

Source code can be found at Github.

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.

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何使用DBFlow进行迁移?   java无法从socket读取数据   java在JButton上使用鼠标运动监听器?   元素的java ArrayList链接和get IndexOutOfBoundsException   javascript如何在Java脚本中从ajax调用解析JSON对象   从数据库中获取信息并将其写入JTable。错误:java。lang.IndexOutOfBoundsException:索引:2,大小:2   spring如何使用Java SpringBoot在FCM推送通知中向IOS发送图像通知   java jsoup:解析某个标记的数据,该标记恰好位于某个特定标记之后   java没有HashMap的等式依赖于EntrySet。钥匙套?   未按预期评估java Maven属性(osmavenplugin)   类Java向ArrayList添加了用户输入,需要用另一个ArrayList中的变量分隔   JavaJSF丰富:每行呈现的数据列表?   集合Java 8按属性区分   在Java中,是否可以(静态)导入构造函数或局部变量?   安卓在ubuntu 10上安装java时出错64位   java是否应该hashCode()只使用equals()中使用的不可变字段的子集?   java如何创建ConcurrentMultiMap