部署環(huán)境:
Centos 6.5 ?/ ? python2.6 ? / ? ?django1.6
1鹃彻、安裝 django-simple-captcha
# pip install?django-simple-captcha==0.4.6
安裝Pillow庫(Pillow-2.6.1)董朝,版本不對會生成不了圖片報錯:
ImportError: The _imagingft C module is not installed
想測試能不能生成圖片可以到我上一篇文章調用生成驗證碼圖片腳本
2角钩、添加captcha到項目的setting.py文件INSTALLED_APPS
CAPTCHA_IMAGE_SIZE = (80, 33)
CAPTCHA_FOREGROUND_COLOR = '#001100'
CAPTCHA_NOISE_FUNCTIONS = ('captcha.helpers.noise_dots',)
CAPTCHA_TEST_MODE = False
3既荚、同步數(shù)據(jù)庫結構
django1.6版本執(zhí)行 python manage.py syncdb
django1.6以后的版本執(zhí)行?python manage.py migrate
4待笑、添加到項目url.py
urlpatterns = [
....
? ? url(r'^captcha/', include('captcha.urls')),
]
5乳幸、forms.py
from django import forms
from captcha.fields import CaptchaField
class CaptchaTestModelForm(forms.Form):
? ? captcha = CaptchaField()
6、添加到視圖view.py(具體根據(jù)自己實際情況配置)
import forms
def some_view(request):
? ? if request.POST:
? ? ? ? form = forms.CaptchaTestForm(request.POST)
? ? ? ? # Validate the form: the captcha field will automatically
? ? ? ? # check the input
? ? ? ? if form.is_valid():
? ? ? ? ? ? human = True
? ? else:
? ? ? ? form = forms.CaptchaTestModelForm()
? ? return render_to_response('template.html',locals())
7瞻坝、添加到template.html
{{ form.captcha?}}
8蛛壳、點擊刷新
關聯(lián)一個JQ文件,然后添加代碼
$('.captcha').click(function(){
? ? ? ? console.log('click');
? ? ? ? $.getJSON("/captcha/refresh/",
? ? ? ? ? ? ? ? ? function(result){
? ? ? ? ? ? $('.captcha').attr('src', result['image_url']);
? ? ? ? ? ? $('#id_captcha_0').val(result['key'])
? ? ? ? ? });
? ? });
官方參考文章:http://django-simple-captcha.readthedocs.io/en/latest/usage.html#installation