Django 点击倒字验证码
Project description
简介
点击倒字验证码,仿照知乎验证码效果实现, 用户通过点击验证码上面的倒着的汉字, 从而通过验证码验证。
环境条件
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 %}
效果图
备注信息
该项目不支持 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.
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file django_click_captcha-0.1.1.tar.gz
.
File metadata
- Download URL: django_click_captcha-0.1.1.tar.gz
- Upload date:
- Size: 276.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0fba57b5b8fac04ef98231f335430c48025d837fe89d3ccef4bcd9f907846209 |
|
MD5 | ec00522dfafa2eb502e37a4c69253e38 |
|
BLAKE2b-256 | afb2a5ebcfdb81dc7203f8e1469740a84580eaca6c224c81be5898e51e3279a8 |