原理:hash_key和用戶輸入的驗(yàn)證碼發(fā)送到數(shù)據(jù)庫作聯(lián)合查詢
1.在項(xiàng)目虛擬環(huán)境中安裝captcha
pip install django-simple-captcha ==0.4.6
2.在setting中設(shè)置
INSTALLED_APPS = ['captcha',]
3.在url中進(jìn)行設(shè)置
url(r'^captcha/', include('captcha.urls')),
4.生成注冊表
python manage.py makemigrations
python manage.py migrate
5.創(chuàng)建forms.py文件,編寫相關(guān)函數(shù)
導(dǎo)入from captcha.fields import CaptchaField
class RegisterForm(forms.Form):
email = forms.EmailField(required=True)
password = forms.CharField(required=True, min_length=5)
captcha = CaptchaField()
設(shè)置中文顯示:captcha = CaptchaField(error_messages={"invalid": u"驗(yàn)證碼錯(cuò)誤"})
6.在views函數(shù)中實(shí)例化并傳入到html頁面
{{ register_form.captche }}