您好,登錄后才能下訂單哦!
Django簡單的驗證碼功能
依賴包
libz-dev libjpeg-dev libfreetype6-dev python-dev
一、下載包,把captcha復(fù)制到項目中
https://github.com/mbi/django-simple-captcha
二、運行
pip install django-simple-captcha
三、添加到settings.py
INSTALLED_APPS = [ 'captcha', ]
四、運行
python manage.py migrate
五、修改urls.py
from django.conf.urls import url,include urlpatterns = [ url(r'^captcha/', include('captcha.urls')), ]
六、修改form
form.py 添加captcha
from captcha.fields import CaptchaField class LoginForm(forms.Form): username = forms.CharField(label='用戶名',widget=forms.TextInput(attrs={"placeholder": "用戶名", "required": "required",}), max_length=50, error_messages={"required": "username不能為空",}) password = forms.CharField(label='密碼',widget=forms.PasswordInput(attrs={"placeholder": "密碼", "required": "required",}), max_length=20, error_messages={"required": "password不能為空",}) captcha = CaptchaField(label='驗證碼') def clean(self): #驗證碼 try: captcha_x = self.cleaned_data['captcha'] except Exception as e: print ('except: ' + str(e)) raise forms.ValidationError(u"驗證碼有誤,請重新輸入")
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。